Skip to content

Commit fef6dd1

Browse files
bnilawarrodrigovivi
authored andcommitted
drm/xe/hwmon: Protect hwmon rw attributes with hwmon_lock
Take hwmon_lock while accessing hwmon rw attributes. For readonly attributes its not required to take lock as reads are protected by sysfs layer and therefore sequential. Cc: Ashutosh Dixit <[email protected]> Cc: Anshuman Gupta <[email protected]> Signed-off-by: Badal Nilawar <[email protected]> Reviewed-by: Anshuman Gupta <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/[email protected] Signed-off-by: Rodrigo Vivi <[email protected]>
1 parent b42ff04 commit fef6dd1

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

drivers/gpu/drm/xe/xe_hwmon.c

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ struct xe_hwmon {
5757
struct device *hwmon_dev;
5858
/** @gt: primary gt */
5959
struct xe_gt *gt;
60-
/** @hwmon_lock: lock for rmw operations */
60+
/** @hwmon_lock: lock for rw attributes*/
6161
struct mutex hwmon_lock;
6262
/** @scl_shift_power: pkg power unit */
6363
int scl_shift_power;
@@ -149,11 +149,13 @@ static void xe_hwmon_power_max_read(struct xe_hwmon *hwmon, long *value)
149149
{
150150
u64 reg_val, min, max;
151151

152+
mutex_lock(&hwmon->hwmon_lock);
153+
152154
xe_hwmon_process_reg(hwmon, REG_PKG_RAPL_LIMIT, REG_READ32, &reg_val, 0, 0);
153155
/* Check if PL1 limit is disabled */
154156
if (!(reg_val & PKG_PWR_LIM_1_EN)) {
155157
*value = PL1_DISABLE;
156-
return;
158+
goto unlock;
157159
}
158160

159161
reg_val = REG_FIELD_GET(PKG_PWR_LIM_1, reg_val);
@@ -167,21 +169,28 @@ static void xe_hwmon_power_max_read(struct xe_hwmon *hwmon, long *value)
167169

168170
if (min && max)
169171
*value = clamp_t(u64, *value, min, max);
172+
unlock:
173+
mutex_unlock(&hwmon->hwmon_lock);
170174
}
171175

172176
static int xe_hwmon_power_max_write(struct xe_hwmon *hwmon, long value)
173177
{
178+
int ret = 0;
174179
u64 reg_val;
175180

181+
mutex_lock(&hwmon->hwmon_lock);
182+
176183
/* Disable PL1 limit and verify, as limit cannot be disabled on all platforms */
177184
if (value == PL1_DISABLE) {
178185
xe_hwmon_process_reg(hwmon, REG_PKG_RAPL_LIMIT, REG_RMW32, &reg_val,
179186
PKG_PWR_LIM_1_EN, 0);
180187
xe_hwmon_process_reg(hwmon, REG_PKG_RAPL_LIMIT, REG_READ32, &reg_val,
181188
PKG_PWR_LIM_1_EN, 0);
182189

183-
if (reg_val & PKG_PWR_LIM_1_EN)
184-
return -EOPNOTSUPP;
190+
if (reg_val & PKG_PWR_LIM_1_EN) {
191+
ret = -EOPNOTSUPP;
192+
goto unlock;
193+
}
185194
}
186195

187196
/* Computation in 64-bits to avoid overflow. Round to nearest. */
@@ -190,8 +199,9 @@ static int xe_hwmon_power_max_write(struct xe_hwmon *hwmon, long value)
190199

191200
xe_hwmon_process_reg(hwmon, REG_PKG_RAPL_LIMIT, REG_RMW32, &reg_val,
192201
PKG_PWR_LIM_1_EN | PKG_PWR_LIM_1, reg_val);
193-
194-
return 0;
202+
unlock:
203+
mutex_unlock(&hwmon->hwmon_lock);
204+
return ret;
195205
}
196206

197207
static void xe_hwmon_power_rated_max_read(struct xe_hwmon *hwmon, long *value)
@@ -229,8 +239,6 @@ xe_hwmon_energy_get(struct xe_hwmon *hwmon, long *energy)
229239
struct xe_hwmon_energy_info *ei = &hwmon->ei;
230240
u64 reg_val;
231241

232-
mutex_lock(&hwmon->hwmon_lock);
233-
234242
xe_hwmon_process_reg(hwmon, REG_PKG_ENERGY_STATUS, REG_READ32,
235243
&reg_val, 0, 0);
236244

@@ -243,8 +251,6 @@ xe_hwmon_energy_get(struct xe_hwmon *hwmon, long *energy)
243251

244252
*energy = mul_u64_u32_shr(ei->accum_energy, SF_ENERGY,
245253
hwmon->scl_shift_energy);
246-
247-
mutex_unlock(&hwmon->hwmon_lock);
248254
}
249255

250256
static const struct hwmon_channel_info *hwmon_info[] = {
@@ -279,12 +285,16 @@ static int xe_hwmon_power_curr_crit_read(struct xe_hwmon *hwmon, long *value, u3
279285
int ret;
280286
u32 uval;
281287

288+
mutex_lock(&hwmon->hwmon_lock);
289+
282290
ret = xe_hwmon_pcode_read_i1(hwmon->gt, &uval);
283291
if (ret)
284-
return ret;
292+
goto unlock;
285293

286294
*value = mul_u64_u32_shr(REG_FIELD_GET(POWER_SETUP_I1_DATA_MASK, uval),
287295
scale_factor, POWER_SETUP_I1_SHIFT);
296+
unlock:
297+
mutex_unlock(&hwmon->hwmon_lock);
288298
return ret;
289299
}
290300

@@ -293,9 +303,12 @@ static int xe_hwmon_power_curr_crit_write(struct xe_hwmon *hwmon, long value, u3
293303
int ret;
294304
u32 uval;
295305

306+
mutex_lock(&hwmon->hwmon_lock);
307+
296308
uval = DIV_ROUND_CLOSEST_ULL(value << POWER_SETUP_I1_SHIFT, scale_factor);
297309
ret = xe_hwmon_pcode_write_i1(hwmon->gt, uval);
298310

311+
mutex_unlock(&hwmon->hwmon_lock);
299312
return ret;
300313
}
301314

0 commit comments

Comments
 (0)