123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704
#include <linux/hwmon-sysfs.h>
#include <linux/hwmon.h>
#include <linux/jiffies.h>
#include <linux/types.h>
#include <linux/units.h>
#include <drm/drm_managed.h>
#include "regs/xe_gt_regs.h"
#include "regs/xe_mchbar_regs.h"
#include "regs/xe_pcode_regs.h"
#include "xe_device.h"
#include "xe_hwmon.h"
#include "xe_log.h"
#include "xe_mmio.h"
#include "xe_pcode.h"
#include "xe_pcode_api.h"
#include "xe_sriov.h"
#include "xe_pm.h"
#include "xe_vsec.h"
#include "regs/xe_pmt.h"
enum xe_hwmon_reg {
REG_TEMP,
REG_PKG_RAPL_LIMIT,
REG_PKG_POWER_SKU,
REG_PKG_POWER_SKU_UNIT,
REG_GT_PERF_STATUS,
REG_PKG_ENERGY_STATUS,
REG_FAN_SPEED,
};
enum xe_hwmon_reg_operation {
REG_READ32,
REG_RMW32,
REG_READ64,
};
#define MAX_VRAM_CHANNELS (16)
enum xe_hwmon_channel {
CHANNEL_CARD,
CHANNEL_PKG,
CHANNEL_VRAM,
CHANNEL_MCTRL,
CHANNEL_PCIE,
CHANNEL_VRAM_N,
CHANNEL_VRAM_N_MAX = CHANNEL_VRAM_N + MAX_VRAM_CHANNELS - 1,
CHANNEL_MAX,
};
enum xe_fan_channel {
FAN_1,
FAN_2,
FAN_3,
FAN_MAX,
};
enum xe_temp_limit {
TEMP_LIMIT_PKG_SHUTDOWN,
TEMP_LIMIT_PKG_CRIT,
TEMP_LIMIT_MEM_SHUTDOWN,
TEMP_LIMIT_PKG_MAX,
TEMP_LIMIT_MEM_CRIT,
TEMP_LIMIT_MAX
};
enum sensor_attr_power {
SENSOR_INDEX_PSYS_PL1,
SENSOR_INDEX_PKG_PL1,
SENSOR_INDEX_PSYS_PL2,
SENSOR_INDEX_PKG_PL2,
};
#define PWR_UNIT 0x3
#define ENERGY_UNIT 0xe
#define TIME_UNIT 0xa
#define SF_POWER 1000000 /* microwatts */
#define SF_CURR 1000 /* milliamperes */
#define SF_VOLTAGE 1000 /* millivolts */
#define SF_ENERGY 1000000 /* microjoules */
#define SF_TIME 1000 /* milliseconds */
#define PL1_HWMON_ATTR hwmon_power_max
#define PL2_HWMON_ATTR hwmon_power_cap
#define PWR_ATTR_TO_STR(attr) (((attr) == hwmon_power_max) ? "PL1" : "PL2")
#define TEMP_INDEX_MCTRL 2
#define TEMP_MBX_INVALID 0xFF
#define TEMP_MBX_SIGN BIT(7)
#define TEMP_MBX_MAG GENMASK(6, 0)
#define MAX_LABEL_SIZE 16
struct xe_hwmon_energy_info {
u32 reg_val_prev;
long accum_energy;
};
struct xe_hwmon_fan_info {
u32 reg_val_prev;
u64 time_prev;
};
struct xe_hwmon_thermal_info {
union {
u8 limit[TEMP_LIMIT_MAX];
__le32 data[DIV_ROUND_UP(TEMP_LIMIT_MAX, sizeof(u32))];
};
u8 count;
union {
u8 value[U8_MAX + 1];
__le32 dword[DIV_ROUND_UP(U8_MAX + 1, sizeof(u32))];
};
char vram_label[MAX_VRAM_CHANNELS][MAX_LABEL_SIZE];
};
struct xe_hwmon {
struct device *hwmon_dev;
struct xe_device *xe;
struct mutex hwmon_lock;
int scl_shift_power;
int scl_shift_energy;
int scl_shift_time;
struct xe_hwmon_energy_info ei[CHANNEL_MAX];
struct xe_hwmon_fan_info fi[FAN_MAX];
bool read_power_limit_from_pcode[CHANNEL_MAX];
u32 pl1_on_boot[CHANNEL_MAX];
u32 pl2_on_boot[CHANNEL_MAX];
struct xe_hwmon_thermal_info temp;
};
static inline int prepare_power_limit_param2(const struct xe_hwmon *hwmon, int channel)
{
if (hwmon->read_power_limit_from_pcode[channel]) {
if (hwmon->xe->info.platform >= XE_CRESCENTISLAND)
return READ_PL_ACCEPTED;
else
return READ_PL_FROM_PCODE;
} else {
return READ_PL_FROM_FW;
}
}
static int xe_hwmon_pcode_read_power_limit(const struct xe_hwmon *hwmon, u32 attr, int channel,
u32 *uval)
{
struct xe_tile *root_tile = xe_device_get_root_tile(hwmon->xe);
u32 val0 = 0, val1 = 0;
int ret = 0;
ret = xe_pcode_read(root_tile, PCODE_MBOX(PCODE_POWER_SETUP,
(channel == CHANNEL_CARD) ?
READ_PSYSGPU_POWER_LIMIT :
READ_PACKAGE_POWER_LIMIT,
prepare_power_limit_param2(hwmon, channel)),
&val0, &val1);
if (ret) {
drm_dbg(&hwmon->xe->drm, "read failed ch %d val0 0x%08x, val1 0x%08x, ret %d\n",
channel, val0, val1, ret);
*uval = 0;
return ret;
}
if (attr == PL1_HWMON_ATTR)
*uval = (val0 & PWR_LIM_EN) ? val0 : 0;
else if (attr == PL2_HWMON_ATTR)
*uval = (val1 & PWR_LIM_EN) ? val1 : 0;
else if (attr == hwmon_power_label)
*uval = (val0 | val1) & PWR_LIM_EN;
else
*uval = 0;
return ret;
}
static int xe_hwmon_pcode_rmw_power_limit(struct xe_hwmon *hwmon, u32 attr, u8 channel,
u32 clr, u32 set)
{
struct xe_tile *root_tile = xe_device_get_root_tile(hwmon->xe);
u32 val0 = 0, val1 = 0;
int ret = 0;
ret = xe_pcode_read(root_tile, PCODE_MBOX(PCODE_POWER_SETUP,
(channel == CHANNEL_CARD) ?
READ_PSYSGPU_POWER_LIMIT :
READ_PACKAGE_POWER_LIMIT,
prepare_power_limit_param2(hwmon, channel)),
&val0, &val1);
if (ret) {
drm_err(&hwmon->xe->drm, "read failed ch %d val0 0x%08x, val1 0x%08x, ret %d\n",
channel, val0, val1, ret);
return ret;
}
if (attr == PL1_HWMON_ATTR)
val0 = (val0 & ~clr) | set;
else if (attr == PL2_HWMON_ATTR)
val1 = (val1 & ~clr) | set;
else
return -EIO;
ret = xe_pcode_write64_timeout(root_tile, PCODE_MBOX(PCODE_POWER_SETUP,
(channel == CHANNEL_CARD) ?
WRITE_PSYSGPU_POWER_LIMIT :
WRITE_PACKAGE_POWER_LIMIT, 0),
val0, val1, PCODE_DEFAULT_TIMEOUT_MS);
if (ret) {
drm_dbg(&hwmon->xe->drm, "write failed ch %d val0 0x%08x, val1 0x%08x, ret %d\n",
channel, val0, val1, ret);
return ret;
}
if (!hwmon->read_power_limit_from_pcode[channel])
hwmon->read_power_limit_from_pcode[channel] = true;
return 0;
}
static int xe_hwmon_pcode_set_default_power_limits(struct xe_hwmon *hwmon)
{
int ret = 0;
ret |= xe_hwmon_pcode_rmw_power_limit(hwmon, PL1_HWMON_ATTR, CHANNEL_CARD,
PWR_LIM | PWR_LIM_TIME,
hwmon->pl1_on_boot[CHANNEL_CARD]);
ret |= xe_hwmon_pcode_rmw_power_limit(hwmon, PL1_HWMON_ATTR, CHANNEL_PKG,
PWR_LIM | PWR_LIM_TIME,
hwmon->pl1_on_boot[CHANNEL_PKG]);
ret |= xe_hwmon_pcode_rmw_power_limit(hwmon, PL2_HWMON_ATTR, CHANNEL_CARD,
PWR_LIM | PWR_LIM_TIME,
hwmon->pl2_on_boot[CHANNEL_CARD]);
ret |= xe_hwmon_pcode_rmw_power_limit(hwmon, PL2_HWMON_ATTR, CHANNEL_PKG,
PWR_LIM | PWR_LIM_TIME,
hwmon->pl2_on_boot[CHANNEL_PKG]);
return ret;
}
static struct xe_reg xe_hwmon_get_reg(struct xe_hwmon *hwmon, enum xe_hwmon_reg hwmon_reg,
int channel)
{
struct xe_device *xe = hwmon->xe;
switch (hwmon_reg) {
case REG_TEMP:
if (xe->info.platform == XE_BATTLEMAGE || xe->info.platform == XE_CRESCENTISLAND) {
if (channel == CHANNEL_PKG)
return BMG_PACKAGE_TEMPERATURE;
else if (channel == CHANNEL_VRAM)
return BMG_VRAM_TEMPERATURE;
else if (in_range(channel, CHANNEL_VRAM_N, MAX_VRAM_CHANNELS))
return BMG_VRAM_TEMPERATURE_N(channel - CHANNEL_VRAM_N);
} else if (xe->info.platform == XE_DG2) {
if (channel == CHANNEL_PKG)
return PCU_CR_PACKAGE_TEMPERATURE;
else if (channel == CHANNEL_VRAM)
return BMG_VRAM_TEMPERATURE;
}
break;
case REG_PKG_RAPL_LIMIT:
if (xe->info.platform == XE_PVC && channel == CHANNEL_PKG)
return PVC_GT0_PACKAGE_RAPL_LIMIT;
else if ((xe->info.platform == XE_DG2) && (channel == CHANNEL_PKG))
return PCU_CR_PACKAGE_RAPL_LIMIT;
break;
case REG_PKG_POWER_SKU:
if (xe->info.platform == XE_PVC && channel == CHANNEL_PKG)
return PVC_GT0_PACKAGE_POWER_SKU;
else if ((xe->info.platform == XE_DG2) && (channel == CHANNEL_PKG))
return PCU_CR_PACKAGE_POWER_SKU;
break;
case REG_PKG_POWER_SKU_UNIT:
if (xe->info.platform == XE_PVC)
return PVC_GT0_PACKAGE_POWER_SKU_UNIT;
else if (xe->info.platform == XE_DG2)
return PCU_CR_PACKAGE_POWER_SKU_UNIT;
break;
case REG_GT_PERF_STATUS:
if (xe->info.platform == XE_DG2 && channel == CHANNEL_PKG)
return GT_PERF_STATUS;
break;
case REG_PKG_ENERGY_STATUS:
if (xe->info.platform == XE_CRESCENTISLAND) {
if (channel == CHANNEL_CARD)
return CRI_PLATFORM_ENERGY_STATUS;
else if (channel == CHANNEL_PKG)
return CRI_PACKAGE_ENERGY_STATUS;
} else if (xe->info.platform == XE_PVC && channel == CHANNEL_PKG) {
return PVC_GT0_PLATFORM_ENERGY_STATUS;
} else if ((xe->info.platform == XE_DG2) && (channel == CHANNEL_PKG)) {
return PCU_CR_PACKAGE_ENERGY_STATUS;
}
break;
case REG_FAN_SPEED:
if (channel == FAN_1)
return BMG_FAN_1_SPEED;
else if (channel == FAN_2)
return BMG_FAN_2_SPEED;
else if (channel == FAN_3)
return BMG_FAN_3_SPEED;
break;
default:
drm_warn(&xe->drm, "Unknown xe hwmon reg id: %d\n", hwmon_reg);
break;
}
return XE_REG(0);
}
#define PL_DISABLE 0
static int xe_hwmon_power_max_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *value)
{
u32 reg_val = 0;
struct xe_device *xe = hwmon->xe;
struct xe_reg rapl_limit, pkg_power_sku;
struct xe_mmio *mmio = xe_root_tile_mmio(xe);
int ret = 0;
mutex_lock(&hwmon->hwmon_lock);
if (hwmon->xe->info.has_mbx_power_limits) {
ret = xe_hwmon_pcode_read_power_limit(hwmon, attr, channel, ®_val);
if (ret)
goto unlock;
} else {
rapl_limit = xe_hwmon_get_reg(hwmon, REG_PKG_RAPL_LIMIT, channel);
pkg_power_sku = xe_hwmon_get_reg(hwmon, REG_PKG_POWER_SKU, channel);
reg_val = xe_mmio_read32(mmio, rapl_limit);
}
if (!(reg_val & PWR_LIM_EN)) {
*value = PL_DISABLE;
drm_info(&hwmon->xe->drm, "%s disabled for channel %d, val 0x%08x\n",
PWR_ATTR_TO_STR(attr), channel, reg_val);
goto unlock;
}
reg_val = REG_FIELD_GET(PWR_LIM_VAL, reg_val);
*value = mul_u32_u32(reg_val, SF_POWER) >> hwmon->scl_shift_power;
if (!hwmon->xe->info.has_mbx_power_limits) {
u64 pkg_pwr, min, max;
pkg_pwr = xe_mmio_read64_2x32(mmio, pkg_power_sku);
min = REG_FIELD_GET(PKG_MIN_PWR, pkg_pwr);
max = REG_FIELD_GET(PKG_MAX_PWR, pkg_pwr);
min = mul_u64_u32_shr(min, SF_POWER, hwmon->scl_shift_power);
max = mul_u64_u32_shr(max, SF_POWER, hwmon->scl_shift_power);
if (min && max)
*value = clamp_t(u64, *value, min, max);
}
unlock:
mutex_unlock(&hwmon->hwmon_lock);
return ret;
}
static int xe_hwmon_power_max_write(struct xe_hwmon *hwmon, u32 attr, int channel, long value)
{
struct xe_mmio *mmio = xe_root_tile_mmio(hwmon->xe);
int ret = 0;
u32 reg_val, max;
struct xe_reg rapl_limit;
u64 max_supp_power_limit = 0;
mutex_lock(&hwmon->hwmon_lock);
rapl_limit = xe_hwmon_get_reg(hwmon, REG_PKG_RAPL_LIMIT, channel);
if (value == PL_DISABLE) {
if (hwmon->xe->info.has_mbx_power_limits) {
drm_dbg(&hwmon->xe->drm, "disabling %s on channel %d\n",
PWR_ATTR_TO_STR(attr), channel);
ret = xe_hwmon_pcode_rmw_power_limit(hwmon, attr, channel, PWR_LIM_EN, 0);
if (ret)
goto unlock;
ret = xe_hwmon_pcode_read_power_limit(hwmon, attr, channel, ®_val);
if (ret)
goto unlock;
} else {
reg_val = xe_mmio_rmw32(mmio, rapl_limit, PWR_LIM_EN, 0);
reg_val = xe_mmio_read32(mmio, rapl_limit);
}
if (reg_val & PWR_LIM_EN) {
drm_warn(&hwmon->xe->drm, "Power limit disable is not supported!\n");
ret = -EOPNOTSUPP;
}
goto unlock;
}
max_supp_power_limit = ((PWR_LIM_VAL) >> hwmon->scl_shift_power) * SF_POWER;
if (value > max_supp_power_limit) {
value = max_supp_power_limit;
drm_info(&hwmon->xe->drm,
"Power limit clamped as selected %s exceeds channel %d limit\n",
PWR_ATTR_TO_STR(attr), channel);
}
reg_val = DIV_ROUND_CLOSEST_ULL((u64)value << hwmon->scl_shift_power, SF_POWER);
if (hwmon->xe->info.has_mbx_power_limits) {
max = (attr == PL1_HWMON_ATTR) ?
hwmon->pl1_on_boot[channel] : hwmon->pl2_on_boot[channel];
max = REG_FIELD_PREP(PWR_LIM_VAL, max);
if (reg_val > max) {
reg_val = max;
drm_dbg(&hwmon->xe->drm,
"Clamping power limit to GPU firmware default 0x%x\n",
reg_val);
}
}
reg_val = PWR_LIM_EN | REG_FIELD_PREP(PWR_LIM_VAL, reg_val);
if (hwmon->xe->info.has_mbx_power_limits)
ret = xe_hwmon_pcode_rmw_power_limit(hwmon, attr, channel, PWR_LIM, reg_val);
else
reg_val = xe_mmio_rmw32(mmio, rapl_limit, PWR_LIM, reg_val);
unlock:
mutex_unlock(&hwmon->hwmon_lock);
return ret;
}
static int xe_hwmon_power_rated_max_read(struct xe_hwmon *hwmon, u32 attr, int channel,
long *value)
{
struct xe_mmio *mmio = xe_root_tile_mmio(hwmon->xe);
u32 reg_val;
int ret = 0;
if (hwmon->xe->info.has_mbx_power_limits) {
ret = xe_hwmon_pcode_read_power_limit(hwmon, PL1_HWMON_ATTR, channel, ®_val);
if (ret)
return ret;
} else {
struct xe_reg reg = xe_hwmon_get_reg(hwmon, REG_PKG_POWER_SKU, channel);
reg_val = xe_mmio_read32(mmio, reg);
}
reg_val = REG_FIELD_GET(PKG_TDP, reg_val);
*value = mul_u64_u32_shr(reg_val, SF_POWER, hwmon->scl_shift_power);
return ret;
}
static int
xe_hwmon_energy_get(struct xe_hwmon *hwmon, int channel, long *energy)
{
struct xe_mmio *mmio = xe_root_tile_mmio(hwmon->xe);
struct xe_hwmon_energy_info *ei = &hwmon->ei[channel];
u32 reg_val;
int ret = 0;
if (channel > CHANNEL_PKG) {
*energy = 0;
return 0;
}
if (hwmon->xe->info.platform == XE_BATTLEMAGE) {
u64 pmt_val;
ret = xe_pmt_telem_read(hwmon->xe->drm.dev,
xe_mmio_read32(mmio, PUNIT_TELEMETRY_GUID),
&pmt_val, BMG_ENERGY_STATUS_PMT_OFFSET, sizeof(pmt_val));
if (ret != sizeof(pmt_val)) {
drm_warn(&hwmon->xe->drm, "energy read from pmt failed, ret %d\n", ret);
*energy = 0;
return ret < 0 ? ret : -EIO;
}
if (channel == CHANNEL_PKG)
reg_val = REG_FIELD_GET64(ENERGY_PKG, pmt_val);
else
reg_val = REG_FIELD_GET64(ENERGY_CARD, pmt_val);
} else {
reg_val = xe_mmio_read32(mmio, xe_hwmon_get_reg(hwmon, REG_PKG_ENERGY_STATUS,
channel));
}
ei->accum_energy += reg_val - ei->reg_val_prev;
ei->reg_val_prev = reg_val;
*energy = mul_u64_u32_shr(ei->accum_energy, SF_ENERGY,
hwmon->scl_shift_energy);
return 0;
}
static ssize_t
xe_hwmon_power_max_interval_show(struct device *dev, struct device_attribute *attr,
char *buf)
{
struct xe_hwmon *hwmon = dev_get_drvdata(dev);
struct xe_mmio *mmio = xe_root_tile_mmio(hwmon->xe);
u32 reg_val, x, y, x_w = 2;
u64 tau4, out;
int channel = (to_sensor_dev_attr(attr)->index % 2) ? CHANNEL_PKG : CHANNEL_CARD;
u32 power_attr = (to_sensor_dev_attr(attr)->index > 1) ? PL2_HWMON_ATTR : PL1_HWMON_ATTR;
int ret = 0;
guard(xe_pm_runtime)(hwmon->xe);
mutex_lock(&hwmon->hwmon_lock);
if (hwmon->xe->info.has_mbx_power_limits) {
ret = xe_hwmon_pcode_read_power_limit(hwmon, power_attr, channel, ®_val);
if (ret) {
xe_log_err(hwmon->xe, HWMON, ret,
"Power interval read fail, ch %d, attr %d, val 0x%08x\n",
channel, power_attr, reg_val);
mutex_unlock(&hwmon->hwmon_lock);
return ret;
}
} else {
reg_val = xe_mmio_read32(mmio, xe_hwmon_get_reg(hwmon, REG_PKG_RAPL_LIMIT,
channel));
}
mutex_unlock(&hwmon->hwmon_lock);
if (hwmon->xe->info.platform >= XE_CRESCENTISLAND) {
reg_val = REG_FIELD_GET(PWR_LIM_TIME, reg_val);
out = (u64)((reg_val >> 2) + ((reg_val & 0x3) >= 2));
} else {
x = REG_FIELD_GET(PWR_LIM_TIME_X, reg_val);
y = REG_FIELD_GET(PWR_LIM_TIME_Y, reg_val);
tau4 = (u64)((1 << x_w) | x) << y;
out = mul_u64_u32_shr(tau4, SF_TIME, hwmon->scl_shift_time + x_w);
}
return sysfs_emit(buf, "%llu\n", out);
}
static ssize_t
xe_hwmon_power_max_interval_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct xe_hwmon *hwmon = dev_get_drvdata(dev);
struct xe_mmio *mmio = xe_root_tile_mmio(hwmon->xe);
u32 x, y, rxy, x_w = 2;
u64 tau4, r, max_win;
unsigned long val;
int channel = (to_sensor_dev_attr(attr)->index % 2) ? CHANNEL_PKG : CHANNEL_CARD;
u32 power_attr = (to_sensor_dev_attr(attr)->index > 1) ? PL2_HWMON_ATTR : PL1_HWMON_ATTR;
int ret;
ret = kstrtoul(buf, 0, &val);
if (ret)
return ret;
#define PKG_MAX_WIN_DEFAULT 0x12ull
r = FIELD_PREP(PKG_MAX_WIN, PKG_MAX_WIN_DEFAULT);
x = REG_FIELD_GET(PKG_MAX_WIN_X, r);
y = REG_FIELD_GET(PKG_MAX_WIN_Y, r);
tau4 = (u64)((1 << x_w) | x) << y;
max_win = mul_u64_u32_shr(tau4, SF_TIME, hwmon->scl_shift_time + x_w);
if (val > max_win)
return -EINVAL;
if (hwmon->xe->info.platform >= XE_CRESCENTISLAND) {
rxy = REG_FIELD_PREP(PWR_LIM_TIME, (val << 2));
} else {
val = DIV_ROUND_CLOSEST_ULL((u64)val << hwmon->scl_shift_time, SF_TIME) + 1;
if (!val) {
y = 0;
x = 0;
} else {
y = ilog2(val);
x = (val - (1ul << y)) << x_w >> y;
}
rxy = REG_FIELD_PREP(PWR_LIM_TIME_X, x) |
REG_FIELD_PREP(PWR_LIM_TIME_Y, y);
}
guard(xe_pm_runtime)(hwmon->xe);
mutex_lock(&hwmon->hwmon_lock);
ret = count;
if (hwmon->xe->info.has_mbx_power_limits) {
ret = xe_hwmon_pcode_rmw_power_limit(hwmon, power_attr, channel, PWR_LIM_TIME, rxy);
if (!ret)
ret = count;
} else {
r = xe_mmio_rmw32(mmio, xe_hwmon_get_reg(hwmon, REG_PKG_RAPL_LIMIT, channel),
PWR_LIM_TIME, rxy);
}
mutex_unlock(&hwmon->hwmon_lock);
return ret;
}
static SENSOR_DEVICE_ATTR(power1_max_interval, 0664,
xe_hwmon_power_max_interval_show,
xe_hwmon_power_max_interval_store, SENSOR_INDEX_PSYS_PL1);
static SENSOR_DEVICE_ATTR(power2_max_interval, 0664,
xe_hwmon_power_max_interval_show,
xe_hwmon_power_max_interval_store, SENSOR_INDEX_PKG_PL1);
static SENSOR_DEVICE_ATTR(power1_cap_interval, 0664,
xe_hwmon_power_max_interval_show,
xe_hwmon_power_max_interval_store, SENSOR_INDEX_PSYS_PL2);
static SENSOR_DEVICE_ATTR(power2_cap_interval, 0664,
xe_hwmon_power_max_interval_show,
xe_hwmon_power_max_interval_store, SENSOR_INDEX_PKG_PL2);
static struct attribute *hwmon_attributes[] = {
&sensor_dev_attr_power1_max_interval.dev_attr.attr,
&sensor_dev_attr_power2_max_interval.dev_attr.attr,
&sensor_dev_attr_power1_cap_interval.dev_attr.attr,
&sensor_dev_attr_power2_cap_interval.dev_attr.attr,
NULL
};
static umode_t xe_hwmon_attributes_visible(struct kobject *kobj,
struct attribute *attr, int index)
{
struct device *dev = kobj_to_dev(kobj);
struct xe_hwmon *hwmon = dev_get_drvdata(dev);
int ret = 0;
int channel = (index % 2) ? CHANNEL_PKG : CHANNEL_CARD;
u32 power_attr = (index > 1) ? PL2_HWMON_ATTR : PL1_HWMON_ATTR;
u32 uval = 0;
struct xe_reg rapl_limit;
struct xe_mmio *mmio = xe_root_tile_mmio(hwmon->xe);
if (hwmon->xe->info.has_mbx_power_limits) {
xe_hwmon_pcode_read_power_limit(hwmon, power_attr, channel, &uval);
} else if (power_attr != PL2_HWMON_ATTR) {
rapl_limit = xe_hwmon_get_reg(hwmon, REG_PKG_RAPL_LIMIT, channel);
if (xe_reg_is_valid(rapl_limit))
uval = xe_mmio_read32(mmio, rapl_limit);
}
ret = (uval & PWR_LIM_EN) ? attr->mode : 0;
return ret;
}
static const struct attribute_group hwmon_attrgroup = {
.attrs = hwmon_attributes,
.is_visible = xe_hwmon_attributes_visible,
};
static const struct attribute_group *hwmon_groups[] = {
&hwmon_attrgroup,
NULL
};
static const struct hwmon_channel_info * const hwmon_info[] = {
HWMON_CHANNEL_INFO(temp,
HWMON_T_LABEL,
HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL |
HWMON_T_MAX,
HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL,
HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL,
HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL,
HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL,
HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL,
HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL,
HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL,
HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL,
HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL,
HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL,
HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL,
HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL,
HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL,
HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL,
HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL,
HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL,
HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL,
HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL,
HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT | HWMON_T_LABEL),
HWMON_CHANNEL_INFO(power, HWMON_P_MAX | HWMON_P_RATED_MAX | HWMON_P_LABEL | HWMON_P_CRIT |
HWMON_P_CAP,
HWMON_P_MAX | HWMON_P_RATED_MAX | HWMON_P_LABEL | HWMON_P_CAP),
HWMON_CHANNEL_INFO(curr, HWMON_C_LABEL, HWMON_C_CRIT | HWMON_C_LABEL),
HWMON_CHANNEL_INFO(in, HWMON_I_INPUT | HWMON_I_LABEL, HWMON_I_INPUT | HWMON_I_LABEL),
HWMON_CHANNEL_INFO(energy, HWMON_E_INPUT | HWMON_E_LABEL, HWMON_E_INPUT | HWMON_E_LABEL),
HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT, HWMON_F_INPUT, HWMON_F_INPUT),
NULL
};
static int xe_hwmon_pcode_read_thermal_info(struct xe_hwmon *hwmon)
{
struct xe_tile *root_tile = xe_device_get_root_tile(hwmon->xe);
u32 config = 0, data0 = 0, data1 = 0;
int ret;
ret = xe_pcode_read(root_tile, PCODE_MBOX(PCODE_THERMAL_INFO, READ_THERMAL_LIMITS, 0),
&data0, &data1);
if (ret)
return ret;
drm_dbg(&hwmon->xe->drm, "thermal info read val 0x%x val1 0x%x\n", data0, data1);
hwmon->temp.data[0] = cpu_to_le32(data0);
hwmon->temp.data[1] = cpu_to_le32(data1);
ret = xe_pcode_read(root_tile, PCODE_MBOX(PCODE_THERMAL_INFO, READ_THERMAL_CONFIG, 0),
&config, NULL);
if (ret)
return ret;
drm_dbg(&hwmon->xe->drm, "thermal config count 0x%x\n", config);
hwmon->temp.count = REG_FIELD_GET(TEMP_MASK, config);
return ret;
}
static inline bool mmio_temp_valid(const struct xe_hwmon *hwmon, u32 value)
{
if (value == U32_MAX)
return false;
return hwmon->xe->info.has_temp_sentinel || value != 0;
}
static inline bool mbx_temp_valid(const struct xe_hwmon *hwmon, u8 value)
{
if (value == TEMP_MBX_INVALID)
return false;
return hwmon->xe->info.has_temp_sentinel || value != 0;
}
static int mbx_temp_decode(u8 value)
{
int temp = value & TEMP_MBX_MAG;
return (value & TEMP_MBX_SIGN) ? -temp : temp;
}
static int get_mc_temp(struct xe_hwmon *hwmon, long *val)
{
struct xe_tile *root_tile = xe_device_get_root_tile(hwmon->xe);
int ret, i, count = 0;
s32 average = 0;
guard(mutex)(&hwmon->hwmon_lock);
for (i = 0; i < DIV_ROUND_UP(hwmon->temp.count, sizeof(u32)); i++) {
u32 data = 0;
ret = xe_pcode_read(root_tile, PCODE_MBOX(PCODE_THERMAL_INFO, READ_THERMAL_DATA, i),
&data, NULL);
if (ret)
return ret;
drm_dbg(&hwmon->xe->drm, "thermal data for group %d val 0x%x\n", i, data);
hwmon->temp.dword[i] = cpu_to_le32(data);
}
for (i = TEMP_INDEX_MCTRL; i < hwmon->temp.count - 1; i++) {
if (!mbx_temp_valid(hwmon, hwmon->temp.value[i]))
continue;
average += mbx_temp_decode(hwmon->temp.value[i]);
count++;
}
if (!count) {
drm_dbg(&hwmon->xe->drm, "Memory controller temperature unavailable!\n");
return -ENXIO;
}
average /= count;
if (val)
*val = average * MILLIDEGREE_PER_DEGREE;
return 0;
}
static int get_pcie_temp(struct xe_hwmon *hwmon, long *val)
{
struct xe_tile *root_tile = xe_device_get_root_tile(hwmon->xe);
u32 data = 0;
int ret;
ret = xe_pcode_read(root_tile, PCODE_MBOX(PCODE_THERMAL_INFO, READ_THERMAL_DATA,
PCIE_SENSOR_GROUP_ID), &data, NULL);
if (ret)
return ret;
if (hwmon->xe->info.subplatform != XE_SUBPLATFORM_BATTLEMAGE_G21)
data = REG_FIELD_GET(PCIE_SENSOR_MASK, data);
data = REG_FIELD_GET(TEMP_MASK, data);
*val = mbx_temp_decode(data) * MILLIDEGREE_PER_DEGREE;
return 0;
}
static int xe_hwmon_pcode_read_i1(const struct xe_hwmon *hwmon, u32 *uval)
{
struct xe_tile *root_tile = xe_device_get_root_tile(hwmon->xe);
if (hwmon->xe->info.platform == XE_DG2)
return -ENXIO;
return xe_pcode_read(root_tile, PCODE_MBOX(PCODE_POWER_SETUP,
POWER_SETUP_SUBCOMMAND_READ_I1, 0),
uval, NULL);
}
static int xe_hwmon_pcode_write_i1(const struct xe_hwmon *hwmon, u32 uval)
{
struct xe_tile *root_tile = xe_device_get_root_tile(hwmon->xe);
return xe_pcode_write(root_tile, PCODE_MBOX(PCODE_POWER_SETUP,
POWER_SETUP_SUBCOMMAND_WRITE_I1, 0),
(uval & POWER_SETUP_I1_DATA_MASK));
}
static int xe_hwmon_pcode_read_fan_control(const struct xe_hwmon *hwmon, u32 subcmd, u32 *uval)
{
struct xe_tile *root_tile = xe_device_get_root_tile(hwmon->xe);
if (hwmon->xe->info.platform == XE_DG2 && subcmd == FSC_READ_NUM_FANS) {
*uval = 2;
return 0;
}
return xe_pcode_read(root_tile, PCODE_MBOX(FAN_SPEED_CONTROL, subcmd, 0), uval, NULL);
}
static int xe_hwmon_power_curr_crit_read(struct xe_hwmon *hwmon, int channel,
long *value, u32 scale_factor)
{
int ret;
u32 uval = 0;
mutex_lock(&hwmon->hwmon_lock);
ret = xe_hwmon_pcode_read_i1(hwmon, &uval);
if (ret)
goto unlock;
*value = mul_u64_u32_shr(REG_FIELD_GET(POWER_SETUP_I1_DATA_MASK, uval),
scale_factor, POWER_SETUP_I1_SHIFT);
unlock:
mutex_unlock(&hwmon->hwmon_lock);
return ret;
}
static int xe_hwmon_power_curr_crit_write(struct xe_hwmon *hwmon, int channel,
long value, u32 scale_factor)
{
int ret;
u32 uval;
u64 max_crit_power_curr = 0;
mutex_lock(&hwmon->hwmon_lock);
max_crit_power_curr = (POWER_SETUP_I1_DATA_MASK >> POWER_SETUP_I1_SHIFT) * scale_factor;
if (value > max_crit_power_curr) {
value = max_crit_power_curr;
drm_info(&hwmon->xe->drm,
"Power limit clamped as selected exceeds channel %d limit\n",
channel);
}
uval = DIV_ROUND_CLOSEST_ULL(value << POWER_SETUP_I1_SHIFT, scale_factor);
ret = xe_hwmon_pcode_write_i1(hwmon, uval);
mutex_unlock(&hwmon->hwmon_lock);
return ret;
}
static void xe_hwmon_get_voltage(struct xe_hwmon *hwmon, int channel, long *value)
{
struct xe_mmio *mmio = xe_root_tile_mmio(hwmon->xe);
u64 reg_val;
reg_val = xe_mmio_read32(mmio, xe_hwmon_get_reg(hwmon, REG_GT_PERF_STATUS, channel));
*value = DIV_ROUND_CLOSEST(REG_FIELD_GET(VOLTAGE_MASK, reg_val) * 2500, SF_VOLTAGE);
}
static inline bool is_vram_ch_available(struct xe_hwmon *hwmon, int channel)
{
struct xe_mmio *mmio = xe_root_tile_mmio(hwmon->xe);
int vram_id = channel - CHANNEL_VRAM_N;
struct xe_reg vram_reg;
vram_reg = xe_hwmon_get_reg(hwmon, REG_TEMP, channel);
if (!xe_reg_is_valid(vram_reg) || !xe_mmio_read32(mmio, vram_reg))
return false;
sprintf(hwmon->temp.vram_label[vram_id], "vram_ch_%d", vram_id);
return true;
}
static umode_t
xe_hwmon_temp_is_visible(struct xe_hwmon *hwmon, u32 attr, int channel)
{
switch (attr) {
case hwmon_temp_emergency:
switch (channel) {
case CHANNEL_PKG:
return hwmon->temp.limit[TEMP_LIMIT_PKG_SHUTDOWN] ? 0444 : 0;
case CHANNEL_VRAM:
return hwmon->temp.limit[TEMP_LIMIT_MEM_SHUTDOWN] ? 0444 : 0;
case CHANNEL_MCTRL:
case CHANNEL_PCIE:
return hwmon->temp.count ? 0444 : 0;
case CHANNEL_VRAM_N...CHANNEL_VRAM_N_MAX:
return (is_vram_ch_available(hwmon, channel) &&
hwmon->temp.limit[TEMP_LIMIT_MEM_SHUTDOWN]) ? 0444 : 0;
default:
return 0;
}
case hwmon_temp_crit:
switch (channel) {
case CHANNEL_PKG:
return hwmon->temp.limit[TEMP_LIMIT_PKG_CRIT] ? 0444 : 0;
case CHANNEL_VRAM:
return hwmon->temp.limit[TEMP_LIMIT_MEM_CRIT] ? 0444 : 0;
case CHANNEL_MCTRL:
case CHANNEL_PCIE:
return hwmon->temp.count ? 0444 : 0;
case CHANNEL_VRAM_N...CHANNEL_VRAM_N_MAX:
return (is_vram_ch_available(hwmon, channel) &&
hwmon->temp.limit[TEMP_LIMIT_MEM_CRIT]) ? 0444 : 0;
default:
return 0;
}
case hwmon_temp_max:
switch (channel) {
case CHANNEL_PKG:
return hwmon->temp.limit[TEMP_LIMIT_PKG_MAX] ? 0444 : 0;
default:
return 0;
}
case hwmon_temp_input:
case hwmon_temp_label:
switch (channel) {
case CHANNEL_PKG:
case CHANNEL_VRAM:
return xe_reg_is_valid(xe_hwmon_get_reg(hwmon, REG_TEMP,
channel)) ? 0444 : 0;
case CHANNEL_MCTRL:
case CHANNEL_PCIE:
return hwmon->temp.count ? 0444 : 0;
case CHANNEL_VRAM_N...CHANNEL_VRAM_N_MAX:
return is_vram_ch_available(hwmon, channel) ? 0444 : 0;
default:
return 0;
}
default:
return 0;
}
}
static int
xe_hwmon_temp_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
{
struct xe_mmio *mmio = xe_root_tile_mmio(hwmon->xe);
u64 reg_val;
switch (attr) {
case hwmon_temp_input:
switch (channel) {
case CHANNEL_PKG:
case CHANNEL_VRAM:
reg_val = xe_mmio_read32(mmio, xe_hwmon_get_reg(hwmon, REG_TEMP, channel));
*val = REG_FIELD_GET(TEMP_MASK, reg_val) * MILLIDEGREE_PER_DEGREE;
return 0;
case CHANNEL_MCTRL:
return get_mc_temp(hwmon, val);
case CHANNEL_PCIE:
return get_pcie_temp(hwmon, val);
case CHANNEL_VRAM_N...CHANNEL_VRAM_N_MAX:
reg_val = xe_mmio_read32(mmio, xe_hwmon_get_reg(hwmon, REG_TEMP, channel));
*val = (s32)(REG_FIELD_GET(TEMP_MASK_VRAM_N, reg_val)) *
(REG_FIELD_GET(TEMP_SIGN_MASK, reg_val) ? -1 : 1) *
MILLIDEGREE_PER_DEGREE;
return 0;
default:
return -EOPNOTSUPP;
}
case hwmon_temp_emergency:
switch (channel) {
case CHANNEL_PKG:
case CHANNEL_MCTRL:
case CHANNEL_PCIE:
*val = hwmon->temp.limit[TEMP_LIMIT_PKG_SHUTDOWN] * MILLIDEGREE_PER_DEGREE;
return 0;
case CHANNEL_VRAM:
case CHANNEL_VRAM_N...CHANNEL_VRAM_N_MAX:
*val = hwmon->temp.limit[TEMP_LIMIT_MEM_SHUTDOWN] * MILLIDEGREE_PER_DEGREE;
return 0;
default:
return -EOPNOTSUPP;
}
case hwmon_temp_crit:
switch (channel) {
case CHANNEL_PKG:
case CHANNEL_MCTRL:
case CHANNEL_PCIE:
*val = hwmon->temp.limit[TEMP_LIMIT_PKG_CRIT] * MILLIDEGREE_PER_DEGREE;
return 0;
case CHANNEL_VRAM:
case CHANNEL_VRAM_N...CHANNEL_VRAM_N_MAX:
*val = hwmon->temp.limit[TEMP_LIMIT_MEM_CRIT] * MILLIDEGREE_PER_DEGREE;
return 0;
default:
return -EOPNOTSUPP;
}
case hwmon_temp_max:
switch (channel) {
case CHANNEL_PKG:
*val = hwmon->temp.limit[TEMP_LIMIT_PKG_MAX] * MILLIDEGREE_PER_DEGREE;
return 0;
default:
return -EOPNOTSUPP;
}
default:
return -EOPNOTSUPP;
}
}
static umode_t
xe_hwmon_power_is_visible(struct xe_hwmon *hwmon, u32 attr, int channel)
{
u32 uval = 0;
struct xe_reg reg;
struct xe_mmio *mmio = xe_root_tile_mmio(hwmon->xe);
switch (attr) {
case hwmon_power_max:
case hwmon_power_cap:
if (hwmon->xe->info.has_mbx_power_limits) {
xe_hwmon_pcode_read_power_limit(hwmon, attr, channel, &uval);
} else if (attr != PL2_HWMON_ATTR) {
reg = xe_hwmon_get_reg(hwmon, REG_PKG_RAPL_LIMIT, channel);
if (xe_reg_is_valid(reg))
uval = xe_mmio_read32(mmio, reg);
}
if (uval & PWR_LIM_EN) {
drm_info(&hwmon->xe->drm, "%s is supported on channel %d\n",
PWR_ATTR_TO_STR(attr), channel);
return 0664;
}
drm_dbg(&hwmon->xe->drm, "%s is unsupported on channel %d\n",
PWR_ATTR_TO_STR(attr), channel);
return 0;
case hwmon_power_rated_max:
if (hwmon->xe->info.has_mbx_power_limits) {
return 0;
} else {
reg = xe_hwmon_get_reg(hwmon, REG_PKG_POWER_SKU, channel);
if (xe_reg_is_valid(reg))
uval = xe_mmio_read32(mmio, reg);
return uval ? 0444 : 0;
}
case hwmon_power_crit:
if (channel == CHANNEL_CARD) {
if (xe_hwmon_pcode_read_i1(hwmon, &uval))
return 0;
return (uval & POWER_SETUP_I1_WATTS) ? 0644 : 0;
}
break;
case hwmon_power_label:
if (hwmon->xe->info.has_mbx_power_limits) {
xe_hwmon_pcode_read_power_limit(hwmon, attr, channel, &uval);
} else {
reg = xe_hwmon_get_reg(hwmon, REG_PKG_POWER_SKU, channel);
if (xe_reg_is_valid(reg))
uval = xe_mmio_read32(mmio, reg);
if (!uval) {
reg = xe_hwmon_get_reg(hwmon, REG_PKG_RAPL_LIMIT, channel);
if (xe_reg_is_valid(reg))
uval = xe_mmio_read32(mmio, reg);
}
}
if ((!(uval & PWR_LIM_EN)) && channel == CHANNEL_CARD) {
if (xe_hwmon_pcode_read_i1(hwmon, &uval))
return 0;
return (uval & POWER_SETUP_I1_WATTS) ? 0444 : 0;
}
return (uval) ? 0444 : 0;
default:
return 0;
}
return 0;
}
static int
xe_hwmon_power_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
{
switch (attr) {
case hwmon_power_max:
case hwmon_power_cap:
return xe_hwmon_power_max_read(hwmon, attr, channel, val);
case hwmon_power_rated_max:
return xe_hwmon_power_rated_max_read(hwmon, attr, channel, val);
case hwmon_power_crit:
return xe_hwmon_power_curr_crit_read(hwmon, channel, val, SF_POWER);
default:
return -EOPNOTSUPP;
}
}
static int
xe_hwmon_power_write(struct xe_hwmon *hwmon, u32 attr, int channel, long val)
{
switch (attr) {
case hwmon_power_cap:
case hwmon_power_max:
return xe_hwmon_power_max_write(hwmon, attr, channel, val);
case hwmon_power_crit:
return xe_hwmon_power_curr_crit_write(hwmon, channel, val, SF_POWER);
default:
return -EOPNOTSUPP;
}
}
static umode_t
xe_hwmon_curr_is_visible(const struct xe_hwmon *hwmon, u32 attr, int channel)
{
u32 uval = 0;
if (channel != CHANNEL_PKG)
return 0;
switch (attr) {
case hwmon_curr_crit:
return (xe_hwmon_pcode_read_i1(hwmon, &uval) ||
(uval & POWER_SETUP_I1_WATTS)) ? 0 : 0644;
case hwmon_curr_label:
return (xe_hwmon_pcode_read_i1(hwmon, &uval) ||
(uval & POWER_SETUP_I1_WATTS)) ? 0 : 0444;
break;
default:
return 0;
}
return 0;
}
static int
xe_hwmon_curr_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
{
switch (attr) {
case hwmon_curr_crit:
return xe_hwmon_power_curr_crit_read(hwmon, channel, val, SF_CURR);
default:
return -EOPNOTSUPP;
}
}
static int
xe_hwmon_curr_write(struct xe_hwmon *hwmon, u32 attr, int channel, long val)
{
switch (attr) {
case hwmon_curr_crit:
return xe_hwmon_power_curr_crit_write(hwmon, channel, val, SF_CURR);
default:
return -EOPNOTSUPP;
}
}
static umode_t
xe_hwmon_in_is_visible(struct xe_hwmon *hwmon, u32 attr, int channel)
{
switch (attr) {
case hwmon_in_input:
case hwmon_in_label:
return xe_reg_is_valid(xe_hwmon_get_reg(hwmon, REG_GT_PERF_STATUS,
channel)) ? 0444 : 0;
default:
return 0;
}
}
static int
xe_hwmon_in_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
{
switch (attr) {
case hwmon_in_input:
xe_hwmon_get_voltage(hwmon, channel, val);
return 0;
default:
return -EOPNOTSUPP;
}
}
static umode_t
xe_hwmon_energy_is_visible(struct xe_hwmon *hwmon, u32 attr, int channel)
{
long energy = 0;
switch (attr) {
case hwmon_energy_input:
case hwmon_energy_label:
if (hwmon->xe->info.platform == XE_BATTLEMAGE) {
xe_hwmon_energy_get(hwmon, channel, &energy);
return energy ? 0444 : 0;
} else {
return xe_reg_is_valid(xe_hwmon_get_reg(hwmon, REG_PKG_ENERGY_STATUS,
channel)) ? 0444 : 0;
}
default:
return 0;
}
}
static int
xe_hwmon_energy_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
{
switch (attr) {
case hwmon_energy_input:
return xe_hwmon_energy_get(hwmon, channel, val);
default:
return -EOPNOTSUPP;
}
}
static umode_t
xe_hwmon_fan_is_visible(struct xe_hwmon *hwmon, u32 attr, int channel)
{
u32 uval = 0;
if (!hwmon->xe->info.has_fan_control)
return 0;
switch (attr) {
case hwmon_fan_input:
if (xe_hwmon_pcode_read_fan_control(hwmon, FSC_READ_NUM_FANS, &uval))
return 0;
return channel < uval ? 0444 : 0;
default:
return 0;
}
}
static int
xe_hwmon_fan_input_read(struct xe_hwmon *hwmon, int channel, long *val)
{
struct xe_mmio *mmio = xe_root_tile_mmio(hwmon->xe);
struct xe_hwmon_fan_info *fi = &hwmon->fi[channel];
u64 rotations, time_now, time;
u32 reg_val;
int ret = 0;
mutex_lock(&hwmon->hwmon_lock);
reg_val = xe_mmio_read32(mmio, xe_hwmon_get_reg(hwmon, REG_FAN_SPEED, channel));
time_now = get_jiffies_64();
rotations = (reg_val - fi->reg_val_prev) / 2;
time = jiffies_delta_to_msecs(time_now - fi->time_prev);
if (unlikely(!time)) {
ret = -EAGAIN;
goto unlock;
}
*val = DIV_ROUND_UP_ULL(rotations * (MSEC_PER_SEC * 60), time);
fi->reg_val_prev = reg_val;
fi->time_prev = time_now;
unlock:
mutex_unlock(&hwmon->hwmon_lock);
return ret;
}
static int
xe_hwmon_fan_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
{
switch (attr) {
case hwmon_fan_input:
return xe_hwmon_fan_input_read(hwmon, channel, val);
default:
return -EOPNOTSUPP;
}
}
static umode_t
xe_hwmon_is_visible(const void *drvdata, enum hwmon_sensor_types type,
u32 attr, int channel)
{
struct xe_hwmon *hwmon = (struct xe_hwmon *)drvdata;
int ret;
switch (type) {
case hwmon_temp:
ret = xe_hwmon_temp_is_visible(hwmon, attr, channel);
break;
case hwmon_power:
ret = xe_hwmon_power_is_visible(hwmon, attr, channel);
break;
case hwmon_curr:
ret = xe_hwmon_curr_is_visible(hwmon, attr, channel);
break;
case hwmon_in:
ret = xe_hwmon_in_is_visible(hwmon, attr, channel);
break;
case hwmon_energy:
ret = xe_hwmon_energy_is_visible(hwmon, attr, channel);
break;
case hwmon_fan:
ret = xe_hwmon_fan_is_visible(hwmon, attr, channel);
break;
default:
ret = 0;
break;
}
return ret;
}
static int
xe_hwmon_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
int channel, long *val)
{
struct xe_hwmon *hwmon = dev_get_drvdata(dev);
guard(xe_pm_runtime)(hwmon->xe);
switch (type) {
case hwmon_temp:
return xe_hwmon_temp_read(hwmon, attr, channel, val);
case hwmon_power:
return xe_hwmon_power_read(hwmon, attr, channel, val);
case hwmon_curr:
return xe_hwmon_curr_read(hwmon, attr, channel, val);
case hwmon_in:
return xe_hwmon_in_read(hwmon, attr, channel, val);
case hwmon_energy:
return xe_hwmon_energy_read(hwmon, attr, channel, val);
case hwmon_fan:
return xe_hwmon_fan_read(hwmon, attr, channel, val);
default:
return -EOPNOTSUPP;
}
}
static int
xe_hwmon_write(struct device *dev, enum hwmon_sensor_types type, u32 attr,
int channel, long val)
{
struct xe_hwmon *hwmon = dev_get_drvdata(dev);
guard(xe_pm_runtime)(hwmon->xe);
switch (type) {
case hwmon_power:
return xe_hwmon_power_write(hwmon, attr, channel, val);
case hwmon_curr:
return xe_hwmon_curr_write(hwmon, attr, channel, val);
default:
return -EOPNOTSUPP;
}
}
static int xe_hwmon_read_label(struct device *dev,
enum hwmon_sensor_types type,
u32 attr, int channel, const char **str)
{
struct xe_hwmon *hwmon = dev_get_drvdata(dev);
switch (type) {
case hwmon_temp:
if (channel == CHANNEL_PKG)
*str = "pkg";
else if (channel == CHANNEL_VRAM)
*str = "vram";
else if (channel == CHANNEL_MCTRL)
*str = "mctrl";
else if (channel == CHANNEL_PCIE)
*str = "pcie";
else if (in_range(channel, CHANNEL_VRAM_N, MAX_VRAM_CHANNELS))
*str = hwmon->temp.vram_label[channel - CHANNEL_VRAM_N];
return 0;
case hwmon_power:
case hwmon_energy:
case hwmon_curr:
case hwmon_in:
if (channel == CHANNEL_CARD)
*str = "card";
else if (channel == CHANNEL_PKG)
*str = "pkg";
return 0;
default:
return -EOPNOTSUPP;
}
}
static const struct hwmon_ops hwmon_ops = {
.is_visible = xe_hwmon_is_visible,
.read = xe_hwmon_read,
.write = xe_hwmon_write,
.read_string = xe_hwmon_read_label,
};
static const struct hwmon_chip_info hwmon_chip_info = {
.ops = &hwmon_ops,
.info = hwmon_info,
};
static void
xe_hwmon_get_preregistration_info(struct xe_hwmon *hwmon)
{
struct xe_mmio *mmio = xe_root_tile_mmio(hwmon->xe);
long energy, fan_speed;
u64 val_sku_unit = 0;
int channel;
struct xe_reg pkg_power_sku_unit;
if (hwmon->xe->info.has_mbx_power_limits) {
if (xe_hwmon_pcode_read_power_limit(hwmon, PL1_HWMON_ATTR, CHANNEL_CARD,
&hwmon->pl1_on_boot[CHANNEL_CARD]) |
xe_hwmon_pcode_read_power_limit(hwmon, PL1_HWMON_ATTR, CHANNEL_PKG,
&hwmon->pl1_on_boot[CHANNEL_PKG]) |
xe_hwmon_pcode_read_power_limit(hwmon, PL2_HWMON_ATTR, CHANNEL_CARD,
&hwmon->pl2_on_boot[CHANNEL_CARD]) |
xe_hwmon_pcode_read_power_limit(hwmon, PL2_HWMON_ATTR, CHANNEL_PKG,
&hwmon->pl2_on_boot[CHANNEL_PKG])) {
drm_warn(&hwmon->xe->drm,
"Failed to read power limits, check GPU firmware !\n");
} else {
drm_info(&hwmon->xe->drm, "Using mailbox commands for power limits\n");
if (xe_hwmon_pcode_set_default_power_limits(hwmon))
drm_warn(&hwmon->xe->drm, "Failed to set default power limits\n");
hwmon->scl_shift_power = PWR_UNIT;
hwmon->scl_shift_energy = ENERGY_UNIT;
hwmon->scl_shift_time = TIME_UNIT;
}
} else {
drm_info(&hwmon->xe->drm, "Using register for power limits\n");
pkg_power_sku_unit = xe_hwmon_get_reg(hwmon, REG_PKG_POWER_SKU_UNIT, 0);
if (xe_reg_is_valid(pkg_power_sku_unit)) {
val_sku_unit = xe_mmio_read32(mmio, pkg_power_sku_unit);
hwmon->scl_shift_power = REG_FIELD_GET(PKG_PWR_UNIT, val_sku_unit);
hwmon->scl_shift_energy = REG_FIELD_GET(PKG_ENERGY_UNIT, val_sku_unit);
hwmon->scl_shift_time = REG_FIELD_GET(PKG_TIME_UNIT, val_sku_unit);
}
}
for (channel = 0; channel < CHANNEL_MAX; channel++)
if (xe_hwmon_is_visible(hwmon, hwmon_energy, hwmon_energy_input, channel))
xe_hwmon_energy_get(hwmon, channel, &energy);
for (channel = 0; channel < FAN_MAX; channel++)
if (xe_hwmon_is_visible(hwmon, hwmon_fan, hwmon_fan_input, channel))
xe_hwmon_fan_input_read(hwmon, channel, &fan_speed);
if (hwmon->xe->info.has_mbx_thermal_info && xe_hwmon_pcode_read_thermal_info(hwmon))
drm_warn(&hwmon->xe->drm, "Thermal mailbox not supported by card firmware\n");
}
int xe_hwmon_register(struct xe_device *xe)
{
struct device *dev = xe->drm.dev;
struct xe_hwmon *hwmon;
int ret, err;
if (!IS_DGFX(xe))
return 0;
if (IS_SRIOV_VF(xe))
return 0;
hwmon = devm_kzalloc_obj(dev, *hwmon);
if (!hwmon)
return -ENOMEM;
ret = devm_mutex_init(dev, &hwmon->hwmon_lock);
if (ret)
return ret;
hwmon->xe = xe;
xe->hwmon = hwmon;
xe_hwmon_get_preregistration_info(hwmon);
drm_dbg(&xe->drm, "Register xe hwmon interface\n");
hwmon->hwmon_dev = devm_hwmon_device_register_with_info(dev, "xe", hwmon,
&hwmon_chip_info,
hwmon_groups);
if (IS_ERR(hwmon->hwmon_dev)) {
err = PTR_ERR(hwmon->hwmon_dev);
xe_log_err(xe, HWMON, err,
"Failed to register xe hwmon\n");
xe->hwmon = NULL;
return err;
}
return 0;
}
MODULE_IMPORT_NS("INTEL_PMT_TELEMETRY");