Repository navigation
Conversation
|
@kv2019i, @juimonen, @ranj063, @RanderWang, @bardliao, @lyakh, if you would take a look at (and perhaps test) this before I take it out from draft. |
|
I don't get why we need to add pull to make ipc work. For IPC4 case, host only sends next ipc message with fw reply, not ack done. |
Yes, but the firmware (SOF) current implementation is that it sends a reply first then at a later time it acks and clears it's BUSY. The change in IRQ handling is to handle a case when we receive the reply message, process it and send a new message to the firmware, but form some reason the Kernel scheduling is still not reached the point to ack the reception of the reply message to the firmware (and mark us not BUSY). So firmware receives the new message but can not send a reply since the host is still BUSY. |
I know the SOF status since I also provided a patch to fix it. Can it be fixed if FW ack first then reply ? |
I would prefer firmware side fix also and I remember we had this discussion a while back and we had a PR, but it got dropped? We have this atm open: thesofproject/sof#6239 But something else is also bugs out with the IPC and I'm not sure what brought out all of these, it used to be pretty stable. |
| u32 hipcidr; | ||
| int ret; | ||
|
|
||
| ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR, |
There was a problem hiding this comment.
@ujfalusi this makes me wonder if we have artificially doubled the IPC timeout for sending and IPC and getting a reply.
The reason I ask is lets say a previous IPC has not been acked by the FW and we're polling here waiting for it and the ack arrives just at the end of the polling time (which is the IPC timeout time), we'd go ahead and send the IPC and wait another ipc_timeout ms to get the reply, right? Should we roll up the send_msg into the original wait we have today instead?
There was a problem hiding this comment.
I have dropped the polling as it does not felt right to spin while we hold spin_lock_irq(), but it would not double the timeout as we are waiting for the ACK for the previous message since the SOF firmware ACKing the processed message after it has sent a reply. Most of the time these are coming at the same time, except when they are not...
| snd_sof_ipc_get_reply(sdev); | ||
| snd_sof_ipc_reply(sdev, data->primary); | ||
|
|
||
| /* Let DSP know that we have finished processing the message */ |
There was a problem hiding this comment.
So, the argument is that we should use the ipc_lock spinlock to ack the received reply? What about the notification, should we use the spinlock there as well?
There was a problem hiding this comment.
Yes, we need to do the acking while holding the lock and it is crucial since otherwise (and really surprisingly) we would somehow proceed to send a new message - the thread which is waiting and the firmware would try to send a reply but the kernel is yet to ack the reception of a previous reply.
It is kind of a some thing as the first patch trying to fix, but in reverse.
The notification is a different thing, I think and we can not hold a lock while handling it since we might send a message as part of handling the notification.
8912710 to
ebf91fe
Compare
|
Changes since v2:
|
|
hi @lyakh , the latest version of ipc tx/rx was designed by you, can you help to review this PR ? thanks |
RanderWang
left a comment
There was a problem hiding this comment.
But when the message is received and processed by the DSP and it wants to
send a reply it will still see that the previous message has not been
acked, so it fails to send a reply. Later the first reply got acked by the
kernel, but it is too late and the in-flight message got a timeout due to
firmware not responding (which it tried, but could not).
if fw can't send a reply immediately , the ipc msg will be inserted in a tx list and will be sent in next schedule time. Why it can't be sent ?
| const struct sof_intel_dsp_desc *chip = hda->desc; | ||
| u32 val; | ||
|
|
||
| val = snd_sof_dsp_read(sdev, HDA_DSP_BAR, chip->ipc_req); |
There was a problem hiding this comment.
is there no internal IPC state indicator to check that we're busy after having sent a request and expecting a response?
There was a problem hiding this comment.
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.
| if (val & chip->ipc_req_mask) | ||
| return false; | ||
|
|
||
| return true; |
There was a problem hiding this comment.
this function is always called as !hda_ipc4_can_send_message() so you could invert its meaning like hda_ipc4_busy() and revert here to return !!(val & chip->ipc_req_mask) or even return !!(chip->ipc_req_mask & snd_sof_dsp_read(sdev, HDA_DSP_BAR, chip->ipc_req)) but that's probably too long. But this is absolutely optional of course
There was a problem hiding this comment.
I was trying to find a descriptive function name for checking that the downstream communication channel is busy, but all came back too long, so ended up with a positive one.
Probably hda_ipc4_send_channel_is_busy() would work better?
I have played with the idea to manage the sdev->ipc->sending_delayed flag in there as well, but that is just overloading a function with a feature you would not have guessed from it's name...
There was a problem hiding this comment.
Or hda_ipc4_tx_channel_is_busy()
| snd_sof_ipc_reply(sdev, data->primary); | ||
|
|
||
| /* Let DSP know that we have finished processing the message */ | ||
| cnl_ipc_host_done(sdev); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
You mean I added response accounting to the firmware to make sure a response is only sent after all processing has completed in all contexts? From that PoV it's only important to keep that promise - to not let the host send new requests before we've completely finished processing the previous one. How exactly the host does that, in which order reply and ack are sent from the DSP - those are questions to the ABI and partially possibly implementation details. As long as serialisation is guaranteed, the firmware should be able to handle IPCs correctly |
Because the delayed tx had been broken.
Because the delayed tx had been broken. |
cba90f0 to
66a6b10
Compare
|
Changes since v4:
|
66a6b10 to
16c92d8
Compare
|
Changes since v5:
|
plbossart
left a comment
There was a problem hiding this comment.
Not the easiest PR to review, I'll need a lot more coffee....
| bool disable_ipc_tx; | ||
|
|
||
| /* The current tx message is delayed due to congestion */ | ||
| bool sending_delayed; |
There was a problem hiding this comment.
it's not clear who delays and what this means. I find the comments lacking clarify on what this is supposed to fix.
There was a problem hiding this comment.
It is not supposed to be fixing anything as such, but it provides a generic flag which can be used by platform code.
By design we always allow one IPC message to be 'in-flight' and it is enforced at high level, platform code relies on this to be enforced.
The flag itself is not used by generic code apart from resetting it before initiating the sending and it is entirely up to the platform code to interpret and use it.
It could be possible to just introduce u8 platform_ipc_flags and use BIT(0) in INtel/IPC4 code for the same purpose to avoid adding a named boolean to the generic IPC struct.
The purpose for this flag is to delay the sending of an IPC message if the communication channel is busy at the moment, but it is going to be free in a short while (we know that because we know how the firmware works).
Instead of outright failing the IPC sending we will delay the actual send itself which is not really a concern for the upper layer as in there we only expect a reply (OK or Fail) or a timeout. Either will happen in any case.
There was a problem hiding this comment.
I think if you use the wording 'delay' there's an implicit notion of a workqueue or deferred processing.
In this case it's really 'send_on_next_irq'. there's no delay that's defined,
There was a problem hiding this comment.
My thinking was that we are delaying the sending of the message until the channel got free.
Initially I added the flag to struct sof_intel_hda_dev but it was a bit out of place, struct sof_ipc4_fw_data is completely out of scope for this kind of runtime handling.
I think I can move the flag down to struct sof_intel_hda_dev and name it differently? But still, the sending is delayed and I don't want to tie it to something concrete to be more reusable.
ipc_tx_blocked (in HDA scope)? The code otherwise would be the same, we will just not touch core code at all.
If the channel is BUSY when we first try to send, we set that the ipc_tx is blocked and we will re-try when the ACK arrives.
There was a problem hiding this comment.
The point is that you don't really wait until the channel is free. You will try to send the message when an interrupt is received on the RX path, possibly for something completely unrelated such as a DSP notification (could even be sets of notifications).
There was a problem hiding this comment.
@plbossart, no, we are waiting for an ACK on the tx channel, so that the tx channel would be free. I deliberately not try to re-send a message when we receive a message (reply or notification).
In my reading the message is delayed for the duration the channel becomes free.
And no, we are not going to try to send a message if we receive a notification, the tx will be only open if we have received a reply (or we have a timeout). Notification will not change this, but we can receive as many notifications as it can fit within the timeout window between we sent a message and received the reply to it.
There was a problem hiding this comment.
not following, you are trying to send in an IRQ thread which could be triggered by the RX channel. We only have one IRQ...
There was a problem hiding this comment.
Yes, we are going to send a message from the irq thread if it was not possible to send when we have tried.
The first version used busy loop polling before sending a message to wait for it becoming free.
For the sending of the message it does not matter if we are in an irq thread or not.
| dev_dbg_ratelimited(sdev->dev, "nothing to do in IPC IRQ thread\n"); | ||
|
|
||
| if (ack_received && sdev->ipc->sending_delayed) | ||
| hda_dsp_ipc4_send_msg(sdev, sdev->msg); |
There was a problem hiding this comment.
what happens if at this point the channel is still busy?
There was a problem hiding this comment.
The message will not be sent and the sending_delayed flag remains set. If a new ACK arrives the sending will be re-tried. The ACK interrupt only comes when the channel becomes free.
If the ACK does not arrive then we will time out the message since we were not able to send it (and get a reply in time).
Next message - if any - is going to be queued up, which is likely to time out as well do to busy channel.
|
|
||
| mtl_ipc_host_done(sdev); | ||
| mtl_ipc_host_done(sdev); | ||
| } |
There was a problem hiding this comment.
I wasn't able to figure out what this change fixes. The commit message is a bit too cryptic to me.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
It makes more sense to log the message before it is sent to the DSP. Signed-off-by: Peter Ujfalusi <peter.ujfalusi@linux.intel.com>
16c92d8 to
0e11230
Compare
|
Changes since v6:
|
…me time Read both registers as the first step in the interrupt handler to make sure that we are handling the event which triggered the interrupt. The delayed reading of the target request register might reflect incorrect information about the reason why the interrupt was risen. Note also that the IPC3 interrupt handler is implemented in this way also. Signed-off-by: Peter Ujfalusi <peter.ujfalusi@linux.intel.com>
…message Before attempting to send a message to the DSP we need to check if the downstream BUSY flag has been cleared by the firmware to avoid lost IPC messages by the firmware. This is required by a firmware which only acks the received message after it has sent a reply to the host. With a bad luck, the host would send a message before the firmware gets to the clearing the flag and thus losing a message. Signed-off-by: Peter Ujfalusi <peter.ujfalusi@linux.intel.com>
By acking a received message we tell the DSP that we have processed the message (reply or notification) and we are open to receive a new one. The original implementation did this in a common code after the received message got handled as reply or notification. With right timing this opens up a small window when we have processed the reply and let the other thread proceed to send a new message to the DSP, which is allowed as the DSP is free to receive message. But when the message is received and processed by the DSP and it wants to send a reply it will still see that the previous message has not been acked, so it fails to send a reply. Later the first reply got acked by the kernel, but it is too late and the in-flight message got a timeout due to firmware not responding (which it tried, but could not). Signed-off-by: Peter Ujfalusi <peter.ujfalusi@linux.intel.com>
0e11230 to
5d11585
Compare
|
Changes since v7:
|
Hi,
patches to address kernel side sequencing and safety checks regarding to IPC sequencing.