Skip to content

cavs-ipc: modify IPC ack handling for IPC4 - #6239

Closed
kv2019i wants to merge 1 commit into
thesofproject:mainfrom
kv2019i:topic/ipc-ack-fix
Closed

kv2019i wants to merge 1 commit into
thesofproject:mainfrom
kv2019i:topic/ipc-ack-fix

Conversation

@kv2019i

@kv2019i kv2019i commented Sep 1, 2022 •

Copy link
Copy Markdown
Collaborator

Note: issue found by @ujfalusi - he has a kernel side fix to the same problem.

In IPC4 protocol, responses are sent over a separate set of
mailbox registers. This creates the following problem:

a) msg received, IPC IRQ
b) IRQ masked
c) handle IPC command, possible in separate core
d) when command handled, send the IPC reply
e) IPC driver complete callback called
f) acknowledge the message (by writing BUSY + DONE)
g) unmask IRQ

The problem is that the acknowledgement in step (f) is
optional part of the protocol, and in fact, driver in IPC4 mode
will consider the reply in step (d) as a signal that it can send
more messages. If driver sends a new message in quick succession,
this may be lost.

This patch addresses the problem by moving the acknowledge
step earlier so that the sequence becomes:

A) msg received, IPC IRQ
B) IRQ masked
C) acknowledge the message (by writing BUSY + DONE)
D) handle IPC command, possible in separate core
E) when command handled, send the IPC reply
F) IPC driver complete callback called
G) unmask IRQ

BugLink: #6173
Signed-off-by: Kai Vehmanen kai.vehmanen@linux.intel.com

In IPC4 protocol, responses are sent over a separate set of
mailbox registers. This creates the following problem:

 a) msg received, IPC IRQ
 b) IRQ masked
 c) handle IPC command, possible in separate core
 d) when command handled, send the IPC reply
 e) IPC driver complete callback called
 f) acknowledge the message (by writing BUSY + DONE)
 g) unmask IRQ

The problem is that the acknowledgement in step (f) is
optional part of the protocol, and in fact, driver in IPC4 mode
will consider the reply in step (d) as a signal that it can send
more messages. If driver sends a new message in quick succession,
this may be lost.

This patch addresses the problem by moving the acknowledge
step earlier so that the sequence becomes:

 A) msg received, IPC IRQ
 B) IRQ masked
 C) acknowledge the message (by writing BUSY + DONE)
 D) handle IPC command, possible in separate core
 E) when command handled, send the IPC reply
 F) IPC driver complete callback called
 G) unmask IRQ

BugLink: thesofproject#6173
Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
@kv2019i

kv2019i commented Sep 1, 2022

Copy link
Copy Markdown
Collaborator Author

Here's my attempt to bridge the gap between IPC semantics in the driver.

I'll keep this as draft until @ujfalusi @lyakh @mmaka1 @ranj063 @marcinszkudlinski @juimonen you've had a chance to review whether this make sense and done more testing.

@kv2019i

kv2019i commented Sep 1, 2022

Copy link
Copy Markdown
Collaborator Author

Tested this internally (Intel test run 15066 ) and seems there are still failures. There's a potential conflict with the newly added mtrace notifications. It seems sending a fw-to-host notification between steps (C) and (E), leads to errors still. I did get locally get 250 iterations through with the notifications enabled, but another 250 iteration run hit an error. Without any fix, a 50 iteration run already hit the error.

@ujfalusi

ujfalusi commented Sep 2, 2022

Copy link
Copy Markdown
Contributor

Note: issue found by @ujfalusi - he has a kernel side fix to the same problem.

In IPC4 protocol, responses are sent over a separate set of mailbox registers. This creates the following problem:

a) msg received, IPC IRQ b) IRQ masked c) handle IPC command, possible in separate core d) when command handled, send the IPC reply e) IPC driver complete callback called f) acknowledge the message (by writing BUSY + DONE) g) unmask IRQ

The problem is that the acknowledgement in step (f) is optional part of the protocol, and in fact, driver in IPC4 mode will consider the reply in step (d) as a signal that it can send more messages. If driver sends a new message in quick succession, this may be lost.

This patch addresses the problem by moving the acknowledge step earlier so that the sequence becomes:

A) msg received, IPC IRQ B) IRQ masked C) acknowledge the message (by writing BUSY + DONE) D) handle IPC command, possible in separate core E) when command handled, send the IPC reply F) IPC driver complete callback called G) unmask IRQ

BugLink: #6173 Signed-off-by: Kai Vehmanen kai.vehmanen@linux.intel.com

@kv2019i, I think the safe sequence would be:
A) msg received, IPC IRQ
B) IRQ masked
C) handle IPC command, possible in separate core
D) when command handled, acknowledge the message (by writing BUSY + DONE)
E) send the IPC reply

F) IPC driver complete callback called
G) unmask IRQ

Iow, reordering C, D and E and the reason is that the ack have other meaning: it is telling the host side that the communication channel is free, the DSP no longer uses the data from the previous message and the host (if it desire to do so) can send a new message.

The problem with the implemented sequence (ACK as the last step, after the reply is sent) is rather racy, especially that the reply sending and ACK is detached and to complicate things we also have delayed replies, and some message (SET_DX) do not even receive an ACK.
It also creates a case when a reply and ACK happens far from each other that we first handle the reply then at later point the ACK. We would need to track two things (which should be not depending on each other) to consider a message sequence completed.

@ujfalusi

ujfalusi commented Sep 2, 2022

Copy link
Copy Markdown
Contributor

Tested this internally (Intel test run 15066 ) and seems there are still failures. There's a potential conflict with the newly added mtrace notifications. It seems sending a fw-to-host notification between steps (C) and (E), leads to errors still. I did get locally get 250 iterations through with the notifications enabled, but another 250 iteration run hit an error. Without any fix, a 50 iteration run already hit the error.

Let me create a draft PR for the kernel side to see how that one deals with the timeouts...

@ujfalusi

ujfalusi commented Sep 2, 2022

Copy link
Copy Markdown
Contributor

@kv2019i, with this thesofproject/linux#3841 we would have a bit more insight on the IPC level of things.

@ujfalusi

ujfalusi commented Sep 2, 2022

Copy link
Copy Markdown
Contributor

Tested this internally (Intel test run 15066 ) and seems there are still failures. There's a potential conflict with the newly added mtrace notifications. It seems sending a fw-to-host notification between steps (C) and (E), leads to errors still. I did get locally get 250 iterations through with the notifications enabled, but another 250 iteration run hit an error. Without any fix, a 50 iteration run already hit the error.

Let me create a draft PR for the kernel side to see how that one deals with the timeouts...

thesofproject/linux#3843


#if CONFIG_IPC_MAJOR_4
#if CAVS_VERSION < CAVS_VERSION_1_8
#error "Invalid configuration for IPC4."

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Are you sure about this? If yes, then it should be a separate patch deprecating CAVS_VERSION_1_5 ?
In current code we only have checks:

#if CAVS_VERSION == CAVS_VERSION_1_5
...
#else
...
#endif 

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.

Probably not, if this approach is ok, maybe we need to keep the 1.5 support around for IPC4.

#if CAVS_VERSION < CAVS_VERSION_1_8
#error "Invalid configuration for IPC4."
#endif
#define IPC4_EARLY_ACK 1 /* ack with DONE immediately */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Basically with this you made sure that IPC4_EARLY_ACK is only defined in case CONFIG_IPC_MAJOR_4 and when CONFIG_IPC_MAJOR_4 is set the IPC4_EARLY_ACK is also. You can just use the CONFIG_IPC_MAJOR_4 in place of IPC4_EARLY_ACK

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.

True, but I kind of prefer to have more meaning in the ifdefs. It makes the code body more readable. Well, avoiding ifdefs to begin with would be even better, but if you have to do it, having some semantics seems helpful. But yeah, style issue.

#endif

/* perform command */
ipc_cmd(hdr);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Right, I see. the reply is sent as a new message within ipc_cmd() at the end of the handling.
In theory host software could rely on the BUSY to decide to send a new message or not, so if we clear it before we processed the message from the registers and mailbox then there is a chance that the host changes the content under our feet...

One way to be safer is to add a flag to struct ipc_msg, something like bool reply_message. If it is true then do the ack before sending the reply message itself. The flag is ignored for IPC3.

@kv2019i

kv2019i commented Sep 2, 2022

Copy link
Copy Markdown
Collaborator Author

@ujfalusi wrote:

@kv2019i wrote:

Tested this internally (Intel test run 15066 ) and seems there are still failures. There's a potential conflict with the newly added mtrace notifications. It seems sending a fw-to-host notification between steps (C) and (E), leads > Let me create a draft PR for the kernel side to see how that one deals with the timeouts...

Ok, this problem was solved -> #6242
It turns out our TX message queueuing has not been working. So if there was contention between the buffer notify and the reply, one would get a EBUSY, be queued, but never sent. And oops, a IPC timeout.

With a fix to this, the tests are now rock solid.

@lgirdwood

Copy link
Copy Markdown
Member

@mwasko fyi - is there a IPC4 expert who could also advise here ?

@mwasko

mwasko commented Sep 2, 2022

Copy link
Copy Markdown
Contributor

@mwasko fyi - is there a IPC4 expert who could also advise here ?

@marcinszkudlinski, @mmaka1 can you provide feedback?

@RanderWang

Copy link
Copy Markdown
Collaborator

is there any problem with MTL ?

@mwasko
mwasko requested a review from pblaszko September 5, 2022 11:04
* do this before we send the response
*/
ipc_write(IPC_DIPCTDR, ipc_read(IPC_DIPCTDR) | IPC_DIPCTDR_BUSY);
ipc_write(IPC_DIPCTDA, ipc_read(IPC_DIPCTDA) | IPC_DIPCTDA_DONE);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

According to cAVS 2.5 HW Arch Spec, IPC flow should be implemented as follows:

  1. DSP receives the interrupt, reads message from DIPCTDR, DIPCTDD and L2 local memory
  2. DSP clears BUSY by writing 1 to DIPCTDR
  3. DSP process the message
  4. DSP set DONE by writing DIPCTDA

Replacing 3. and 4. will violate the architecture flow.
Note: in closed-source cAVS, BUSY is cleared together with set DONE, after processing the message, so moment of clearing BUSY seems to not to be critical.

At present cAVS closed-source solution, BUSY and DONE are triggered after processing message from host and before writing response. It seems to be the best way.

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.

Thanks @pblaszko ! Indeed, this is what @ujfalusi also commented in last Friday in #6239 (comment) . And yes, this seems to be the correct thing.

The key seems to be whether busy+done clearance is done before or after the response is sent. We are err'ing towards the direction that host/kernel should handle even the case where busy+done is cleared only at the end (i.e. the original sequence).

@pblaszko

pblaszko commented Sep 5, 2022

Copy link
Copy Markdown
Contributor

In IPC4 protocol, responses are sent over a separate set of mailbox registers. This creates the following problem:

a) msg received, IPC IRQ b) IRQ masked c) handle IPC command, possible in separate core d) when command handled, send the IPC reply e) IPC driver complete callback called f) acknowledge the message (by writing BUSY + DONE) g) unmask IRQ

The problem is that the acknowledgement in step (f) is optional part of the protocol, and in fact, driver in IPC4 mode will consider the reply in step (d) as a signal that it can send more messages. If driver sends a new message in quick succession, this may be lost.

@kv2019i do you mean host driver? Host driver should not consider (d) as IPC completion. After receiving reply from DSP, driver should set HIPCTDA.DONE, and still wait until DSP clears it. Only then Host driver may send next message to DSP. Maybe the problem you're observing lays in driver, not FW?

@kv2019i

kv2019i commented Sep 5, 2022

Copy link
Copy Markdown
Collaborator Author

@pblaszko wrote:

@kv2019i do you mean host driver? Host driver should not consider (d) as IPC completion. After receiving reply from DSP, driver should set HIPCTDA.DONE, and still wait until DSP clears it. Only then Host driver may send next message to DSP. Maybe the problem you're observing lays in driver, not FW?

Ok thanks. And yes, I was talking of the host kernel driver, and indeed this seems to make sense. @ujfalusi is now making the changes in thesofproject/linux#3843 . @ujfalusi please check the above

@kv2019i

kv2019i commented Sep 6, 2022

Copy link
Copy Markdown
Collaborator Author

Concluding this PR. Basicly in short:

Let's first get the kernel changes in, and submit a separate PR to enhance the FW implementation. Closing this one.

@kv2019i kv2019i closed this Sep 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants