Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions sound/soc/sof/intel/cnl.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ irqreturn_t cnl_ipc4_irq_thread(int irq, void *context)
{
struct sof_ipc4_msg notification_data = {{ 0 }};
struct snd_sof_dev *sdev = context;
bool ack_received = false;
Comment thread
plbossart marked this conversation as resolved.
Resolved
bool ipc_irq = false;
u32 hipcida, hipctdr;

hipcida = snd_sof_dsp_read(sdev, HDA_DSP_BAR, CNL_DSP_REG_HIPCIDA);
hipctdr = snd_sof_dsp_read(sdev, HDA_DSP_BAR, CNL_DSP_REG_HIPCTDR);
Comment thread
plbossart marked this conversation as resolved.
Resolved
Comment thread
plbossart marked this conversation as resolved.
Resolved
if (hipcida & CNL_DSP_REG_HIPCIDA_DONE) {
/* DSP received the message */
snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR,
Expand All @@ -49,9 +51,9 @@ irqreturn_t cnl_ipc4_irq_thread(int irq, void *context)
cnl_ipc_dsp_done(sdev);

ipc_irq = true;
ack_received = true;
}

hipctdr = snd_sof_dsp_read(sdev, HDA_DSP_BAR, CNL_DSP_REG_HIPCTDR);
if (hipctdr & CNL_DSP_REG_HIPCTDR_BUSY) {
/* Message from DSP (reply or notification) */
u32 hipctdd = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
Expand All @@ -70,6 +72,7 @@ irqreturn_t cnl_ipc4_irq_thread(int irq, void *context)
spin_lock_irq(&sdev->ipc_lock);

snd_sof_ipc_get_reply(sdev);
cnl_ipc_host_done(sdev);

ghost Sep 6, 2022

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. But does this close the race window completely? What if a reply arrives, but the sending thread sends a new message from the host to the DSP before we ack here? Maybe even before the interrupt triggers?

ghost Sep 6, 2022

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can not have two tx messages at the same time. they are serialized with a higher level mutex, the next message can only proceed if the previous got the reply.
The problem is that we have multiple races in firmware as well and the two independent channels are made dependent of each other.

No, the checking of the tx channel that it is free is not going to fix this race as there is still (I have seen that with only the busy patch) that the tx channel from host was free, we sent a message, fw handled and tried to send a reply, but the kernel still have not managed to ack the reply. The fw delayed message handling is broken, so we ended up with IPC timeout.

snd_sof_ipc_reply(sdev, data->primary);

spin_unlock_irq(&sdev->ipc_lock);
Expand All @@ -86,10 +89,10 @@ irqreturn_t cnl_ipc4_irq_thread(int irq, void *context)
sdev->ipc->msg.rx_data = &notification_data;
snd_sof_ipc_msgs_rx(sdev);
sdev->ipc->msg.rx_data = NULL;
}

/* Let DSP know that we have finished processing the message */
cnl_ipc_host_done(sdev);
/* Let DSP know that we have finished processing the message */
cnl_ipc_host_done(sdev);
}

ipc_irq = true;
}
Expand All @@ -98,6 +101,13 @@ irqreturn_t cnl_ipc4_irq_thread(int irq, void *context)
/* This interrupt is not shared so no need to return IRQ_NONE. */
dev_dbg_ratelimited(sdev->dev, "nothing to do in IPC IRQ thread\n");

if (ack_received) {
struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;

if (hdev->delayed_ipc_tx_msg)
cnl_ipc4_send_msg(sdev, hdev->delayed_ipc_tx_msg);
}

return IRQ_HANDLED;
}

Expand Down Expand Up @@ -251,8 +261,16 @@ static bool cnl_compact_ipc_compress(struct snd_sof_ipc_msg *msg,

int cnl_ipc4_send_msg(struct snd_sof_dev *sdev, struct snd_sof_ipc_msg *msg)
{
struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
struct sof_ipc4_msg *msg_data = msg->msg_data;

if (hda_ipc4_tx_is_busy(sdev)) {
hdev->delayed_ipc_tx_msg = msg;
return 0;
}

hdev->delayed_ipc_tx_msg = NULL;

/* send the message via mailbox */
if (msg_data->data_size)
sof_mailbox_write(sdev, sdev->host_box.offset, msg_data->data_ptr,
Expand Down
27 changes: 23 additions & 4 deletions sound/soc/sof/intel/hda-ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,16 @@ int hda_dsp_ipc_send_msg(struct snd_sof_dev *sdev, struct snd_sof_ipc_msg *msg)

int hda_dsp_ipc4_send_msg(struct snd_sof_dev *sdev, struct snd_sof_ipc_msg *msg)
{
struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
struct sof_ipc4_msg *msg_data = msg->msg_data;

if (hda_ipc4_tx_is_busy(sdev)) {
hdev->delayed_ipc_tx_msg = msg;
return 0;
}

hdev->delayed_ipc_tx_msg = NULL;

/* send the message via mailbox */
if (msg_data->data_size)
sof_mailbox_write(sdev, sdev->host_box.offset, msg_data->data_ptr,
Expand Down Expand Up @@ -122,20 +130,23 @@ irqreturn_t hda_dsp_ipc4_irq_thread(int irq, void *context)
{
struct sof_ipc4_msg notification_data = {{ 0 }};
struct snd_sof_dev *sdev = context;
bool ack_received = false;
bool ipc_irq = false;
u32 hipcie, hipct;

hipcie = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCIE);
hipct = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCT);

if (hipcie & HDA_DSP_REG_HIPCIE_DONE) {
/* DSP received the message */
snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCCTL,
HDA_DSP_REG_HIPCCTL_DONE, 0);
hda_dsp_ipc_dsp_done(sdev);

ipc_irq = true;
ack_received = true;
}

hipct = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCT);
if (hipct & HDA_DSP_REG_HIPCT_BUSY) {
/* Message from DSP (reply or notification) */
u32 hipcte = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
Expand All @@ -158,6 +169,7 @@ irqreturn_t hda_dsp_ipc4_irq_thread(int irq, void *context)
spin_lock_irq(&sdev->ipc_lock);

snd_sof_ipc_get_reply(sdev);
hda_dsp_ipc_host_done(sdev);
snd_sof_ipc_reply(sdev, data->primary);

spin_unlock_irq(&sdev->ipc_lock);
Expand All @@ -174,10 +186,10 @@ irqreturn_t hda_dsp_ipc4_irq_thread(int irq, void *context)
sdev->ipc->msg.rx_data = &notification_data;
snd_sof_ipc_msgs_rx(sdev);
sdev->ipc->msg.rx_data = NULL;
}

/* Let DSP know that we have finished processing the message */
hda_dsp_ipc_host_done(sdev);
/* Let DSP know that we have finished processing the message */
hda_dsp_ipc_host_done(sdev);
}

ipc_irq = true;
}
Expand All @@ -186,6 +198,13 @@ irqreturn_t hda_dsp_ipc4_irq_thread(int irq, void *context)
/* This interrupt is not shared so no need to return IRQ_NONE. */
dev_dbg_ratelimited(sdev->dev, "nothing to do in IPC IRQ thread\n");

if (ack_received) {
struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;

if (hdev->delayed_ipc_tx_msg)
hda_dsp_ipc4_send_msg(sdev, hdev->delayed_ipc_tx_msg);
}

return IRQ_HANDLED;
}

Expand Down
11 changes: 11 additions & 0 deletions sound/soc/sof/intel/hda.c
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,17 @@ void hda_ipc4_dump(struct snd_sof_dev *sdev)
hipci, hipcie, hipct, hipcte, hipcctl);
}

bool hda_ipc4_tx_is_busy(struct snd_sof_dev *sdev)
{
struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
const struct sof_intel_dsp_desc *chip = hda->desc;
u32 val;

val = snd_sof_dsp_read(sdev, HDA_DSP_BAR, chip->ipc_req);

ghost Sep 6, 2022

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there no internal IPC state indicator to check that we're busy after having sent a request and expecting a response?

ghost Sep 6, 2022

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the hardware handshake between the two processors.
Initially the assumption was that when the DSP sent a reply it is free to receive a message. This turned out to be not the case: the SOF firmware is acking the message after it sent the reply message, which is fine most of the time... But there are cases when the time between the reply sending and acking of the received message (the one the reply was sent) is so long that the kernel might send another message (without checking that the channel is free), the firmware get to the point to ack the previous message and it actually acks the current one, loosing it completely.

We are doing to be safe even if the DSP does something unexpected, but not totally invalid.


return !!(val & chip->ipc_req_mask);
}

static int hda_init(struct snd_sof_dev *sdev)
{
struct hda_bus *hbus;
Expand Down
9 changes: 9 additions & 0 deletions sound/soc/sof/intel/hda.h
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,14 @@ struct sof_intel_hda_dev {

/* Intel NHLT information */
struct nhlt_acpi_table *nhlt;

/*
* Pointing to the IPC message if immediate sending was not possible
* because the downlink communication channel was BUSY at the time.
* The message will be re-tried when the channel becomes free (the ACK
* is received from the DSP for the previous message)
*/
struct snd_sof_ipc_msg *delayed_ipc_tx_msg;
};

static inline struct hdac_bus *sof_to_bus(struct snd_sof_dev *s)
Expand Down Expand Up @@ -853,6 +861,7 @@ int hda_dsp_core_stall_reset(struct snd_sof_dev *sdev, unsigned int core_mask);
irqreturn_t cnl_ipc4_irq_thread(int irq, void *context);
int cnl_ipc4_send_msg(struct snd_sof_dev *sdev, struct snd_sof_ipc_msg *msg);
irqreturn_t hda_dsp_ipc4_irq_thread(int irq, void *context);
bool hda_ipc4_tx_is_busy(struct snd_sof_dev *sdev);
int hda_dsp_ipc4_send_msg(struct snd_sof_dev *sdev, struct snd_sof_ipc_msg *msg);
void hda_ipc4_dump(struct snd_sof_dev *sdev);
extern struct sdw_intel_ops sdw_callback;
Expand Down
24 changes: 21 additions & 3 deletions sound/soc/sof/intel/mtl.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,16 @@ static bool mtl_dsp_check_sdw_irq(struct snd_sof_dev *sdev)

static int mtl_ipc_send_msg(struct snd_sof_dev *sdev, struct snd_sof_ipc_msg *msg)
{
struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
struct sof_ipc4_msg *msg_data = msg->msg_data;

if (hda_ipc4_tx_is_busy(sdev)) {
hdev->delayed_ipc_tx_msg = msg;
return 0;
}

hdev->delayed_ipc_tx_msg = NULL;

/* send the message via mailbox */
if (msg_data->data_size)
sof_mailbox_write(sdev, sdev->host_box.offset, msg_data->data_ptr,
Expand Down Expand Up @@ -492,11 +500,13 @@ static irqreturn_t mtl_ipc_irq_thread(int irq, void *context)
{
struct sof_ipc4_msg notification_data = {{ 0 }};
struct snd_sof_dev *sdev = context;
bool ack_received = false;
bool ipc_irq = false;
u32 hipcida;
u32 hipctdr;

hipcida = snd_sof_dsp_read(sdev, HDA_DSP_BAR, MTL_DSP_REG_HFIPCXIDA);
hipctdr = snd_sof_dsp_read(sdev, HDA_DSP_BAR, MTL_DSP_REG_HFIPCXTDR);

/* reply message from DSP */
if (hipcida & MTL_DSP_REG_HFIPCXIDA_DONE) {
Expand All @@ -507,9 +517,9 @@ static irqreturn_t mtl_ipc_irq_thread(int irq, void *context)
mtl_ipc_dsp_done(sdev);

ipc_irq = true;
ack_received = true;
}

hipctdr = snd_sof_dsp_read(sdev, HDA_DSP_BAR, MTL_DSP_REG_HFIPCXTDR);
if (hipctdr & MTL_DSP_REG_HFIPCXTDR_BUSY) {
/* Message from DSP (reply or notification) */
u32 extension = snd_sof_dsp_read(sdev, HDA_DSP_BAR, MTL_DSP_REG_HFIPCXTDDY);
Expand All @@ -530,6 +540,7 @@ static irqreturn_t mtl_ipc_irq_thread(int irq, void *context)
spin_lock_irq(&sdev->ipc_lock);

snd_sof_ipc_get_reply(sdev);
mtl_ipc_host_done(sdev);
Comment thread
RanderWang marked this conversation as resolved.
Resolved
snd_sof_ipc_reply(sdev, data->primary);

spin_unlock_irq(&sdev->ipc_lock);
Expand All @@ -546,9 +557,9 @@ static irqreturn_t mtl_ipc_irq_thread(int irq, void *context)
sdev->ipc->msg.rx_data = &notification_data;
snd_sof_ipc_msgs_rx(sdev);
sdev->ipc->msg.rx_data = NULL;
}

mtl_ipc_host_done(sdev);
mtl_ipc_host_done(sdev);
}

ghost Sep 12, 2022

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't able to figure out what this change fixes. The commit message is a bit too cryptic to me.

ghost Sep 12, 2022

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sequence which I have seen twice:

  • Linux sent a message
  • fw sent a reply to the message
  • Linux received the reply, processed and completed the TX
  • Linux was open to send a new message
  • The downlink channel was open (fw acked the previous message)
  • Linux sent a new message
  • fw received a message, processed and tried to reply, but the uplink channel was still marked busy.
  • fw delayed the sending of the reply (the delyed sending was/is broken in firmware)
  • Linux finally arrived to the point to ack the received reply
  • now fw should be able to send a new message, but the re-sending was broken -> we had IPC timeout due to missing reply.

ghost Sep 12, 2022

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving the reply acking fixes this by making sure that we only send a new message after we have certainly acked the reception of the reply.

With the fix on the firmware side, this should not be needed anymore, but I rather play safe.


ipc_irq = true;
}
Expand All @@ -558,6 +569,13 @@ static irqreturn_t mtl_ipc_irq_thread(int irq, void *context)
dev_dbg_ratelimited(sdev->dev, "nothing to do in IPC IRQ thread\n");
}

if (ack_received) {
struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;

if (hdev->delayed_ipc_tx_msg)
mtl_ipc_send_msg(sdev, hdev->delayed_ipc_tx_msg);
}

return IRQ_HANDLED;
}

Expand Down
4 changes: 2 additions & 2 deletions sound/soc/sof/ipc4.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ static int ipc4_tx_msg_unlocked(struct snd_sof_ipc *ipc,
if (msg_bytes > ipc->max_payload_size || reply_bytes > ipc->max_payload_size)
return -EINVAL;

sof_ipc4_log_header(sdev->dev, "ipc tx ", msg_data, true);
Comment thread
plbossart marked this conversation as resolved.
Resolved

ret = sof_ipc_send_msg(sdev, msg_data, msg_bytes, reply_bytes);
if (ret) {
dev_err_ratelimited(sdev->dev,
Expand All @@ -350,8 +352,6 @@ static int ipc4_tx_msg_unlocked(struct snd_sof_ipc *ipc,
return ret;
}

sof_ipc4_log_header(sdev->dev, "ipc tx ", msg_data, true);

/* now wait for completion */
return ipc4_wait_tx_done(ipc, reply_data);
}
Expand Down