From: John Iliopoulos On AMD MP2 v2 platforms (observed on HP ENVY x360 Convertible 15-eu0xxx with BIOS F.18), enabling or disabling a sensor can cause MP2 firmware to also enable or disable an internal companion sensor immediately before or after the requested sensor: - Enabling gyroscope (sid 0x1) emits 0x00150348 (sid 0x15) alongside 0x00010340 (sid 0x1). - Enabling or disabling operating-mode (sid 0xf) emits 0x000d0348 or 0x000d0358 (sid 0xd) alongside 0x000f0340 or 0x000f0350 (sid 0xf). Because MP2 v2 reports command completions in a single AMD_P2C_MSG(0) register rather than a FIFO, when the companion sensor finishes after the requested sensor (or while the host is sleeping in the 500 us poll interval or preempted), it overwrites AMD_P2C_MSG(0). amd_sfh_wait_response_v2() then misses the requested sensor_id and spins for the full 10-second timeout, falsely marking gyro_3d or operating-mode (and therefore amd_sfh_tabletmode) as disabled, or stalling driver removal and suspend for 10 seconds. Capture the previous AMD_P2C_MSG(0) value before issuing start/stop commands, tighten the poll interval to 50 us, and when AMD_P2C_MSG(0) transitions to a valid (status == 0) matching response for a different sensor ID, wait briefly (50 ms) in case the requested sensor's response is still arriving. If it was already overwritten, re-write AMD_C2P_MSG0 once so MP2 reports completion for the requested sensor. Fixes: 173709f50e98 ("HID: amd_sfh: Add command response to check command status") Cc: stable@vger.kernel.org Signed-off-by: John Iliopoulos --- drivers/hid/amd-sfh-hid/amd_sfh_common.h | 2 ++ drivers/hid/amd-sfh-hid/amd_sfh_pcie.c | 62 ++++++++++++++++++++++++++++---- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/drivers/hid/amd-sfh-hid/amd_sfh_common.h b/drivers/hid/amd-sfh-hid/amd_sfh_common.h index 4481a50359de..4c5ed184d9f2 100644 --- a/drivers/hid/amd-sfh-hid/amd_sfh_common.h +++ b/drivers/hid/amd-sfh-hid/amd_sfh_common.h @@ -70,6 +70,8 @@ struct amd_mp2_dev { struct amd_input_data in_data; /* mp2 active control status */ u32 mp2_acs; + /* P2C_MSG0 value captured before sending the current command */ + u32 prev_p2c0; struct sfh_dev_status dev_en; struct work_struct work; /* mp2 to protect data */ diff --git a/drivers/hid/amd-sfh-hid/amd_sfh_pcie.c b/drivers/hid/amd-sfh-hid/amd_sfh_pcie.c index f70669bb403e..a2ddd1b5b1e5 100644 --- a/drivers/hid/amd-sfh-hid/amd_sfh_pcie.c +++ b/drivers/hid/amd-sfh-hid/amd_sfh_pcie.c @@ -44,16 +44,64 @@ static bool intr_disable = true; module_param_named(intr_disable, intr_disable, bool, 0444); MODULE_PARM_DESC(intr_disable, "override the interrupt disable sensor bit"); +#define AMD_SFH_POLL_INTERVAL_US 50 +#define AMD_SFH_COMPANION_WAIT_US 50000 +#define AMD_SFH_RESP_TIMEOUT_US 10000000 + +static bool amd_sfh_resp_v2_matches(union cmd_response resp, u8 sid, u32 sensor_sts) +{ + return resp.response_v2.response == sensor_sts && + resp.response_v2.status == 0 && + (sid == 0xff || resp.response_v2.sensor_id == sid); +} + +static bool amd_sfh_resp_v2_is_companion(union cmd_response resp, u32 prev, + u8 sid, u32 sensor_sts) +{ + return resp.resp != prev && + resp.response_v2.response == sensor_sts && + resp.response_v2.status == 0 && + sid != 0xff && resp.response_v2.sensor_id != sid; +} + static int amd_sfh_wait_response_v2(struct amd_mp2_dev *mp2, u8 sid, u32 sensor_sts) { union cmd_response cmd_resp; + u32 prev_p2c0 = mp2->prev_p2c0; + bool retried = false; - /* Get response with status within a max of 10 seconds timeout */ - if (!readl_poll_timeout(mp2->mmio + AMD_P2C_MSG(0), cmd_resp.resp, - (cmd_resp.response_v2.response == sensor_sts && - cmd_resp.response_v2.status == 0 && (sid == 0xff || - cmd_resp.response_v2.sensor_id == sid)), 500, 10000000)) - return cmd_resp.response_v2.response; + for (;;) { + if (!readl_poll_timeout(mp2->mmio + AMD_P2C_MSG(0), cmd_resp.resp, + amd_sfh_resp_v2_matches(cmd_resp, sid, sensor_sts) || + (!retried && + amd_sfh_resp_v2_is_companion(cmd_resp, prev_p2c0, + sid, sensor_sts)), + AMD_SFH_POLL_INTERVAL_US, AMD_SFH_RESP_TIMEOUT_US)) { + if (amd_sfh_resp_v2_matches(cmd_resp, sid, sensor_sts)) + return cmd_resp.response_v2.response; + + /* + * MP2 firmware may emit a response for an internal companion + * sensor just before or after the requested sensor, overwriting + * P2C_MSG0. Wait briefly in case the requested response is still + * arriving, otherwise re-trigger the command once. + */ + if (!readl_poll_timeout(mp2->mmio + AMD_P2C_MSG(0), cmd_resp.resp, + amd_sfh_resp_v2_matches(cmd_resp, sid, sensor_sts), + AMD_SFH_POLL_INTERVAL_US, + AMD_SFH_COMPANION_WAIT_US)) + return cmd_resp.response_v2.response; + + dev_dbg(&mp2->pdev->dev, + "sid 0x%x response overwritten by 0x%08x, retrying\n", + sid, cmd_resp.resp); + writel(readl(mp2->mmio + AMD_C2P_MSG0), mp2->mmio + AMD_C2P_MSG0); + retried = true; + continue; + } + + break; + } return SENSOR_DISABLED; } @@ -72,6 +120,7 @@ static void amd_start_sensor_v2(struct amd_mp2_dev *privdata, struct amd_mp2_sen if (info.sensor_idx == als_idx) cmd_base.cmd_v2.mem_type = USE_C2P_REG; + privdata->prev_p2c0 = readl(privdata->mmio + AMD_P2C_MSG(0)); writeq(info.dma_address, privdata->mmio + AMD_C2P_MSG1); writel(cmd_base.ul, privdata->mmio + AMD_C2P_MSG0); } @@ -87,6 +136,7 @@ static void amd_stop_sensor_v2(struct amd_mp2_dev *privdata, u16 sensor_idx) cmd_base.cmd_v2.sensor_id = sensor_idx; cmd_base.cmd_v2.length = 16; + privdata->prev_p2c0 = readl(privdata->mmio + AMD_P2C_MSG(0)); writeq(0x0, privdata->mmio + AMD_C2P_MSG1); writel(cmd_base.ul, privdata->mmio + AMD_C2P_MSG0); } -- 2.56.1