: powerpc documentation: Clarify why twi appears in the i/o macros.
PATCH 1/1 : powerpc documentation: Clarify why twi appears in the i/o macros.
Paul,
Please apply. This atch resulted from an email discussion back in Sept.
--linas
Clarify why twi appears in the i/o macros.
----
include/asm-powerpc/io.h | 11 +++++++++++ 1 file changed, 11 insertions(+)
include/asm-powerpc/io.h +11 -0
@@ -78,6 +78,17 @@ extern unsigned long pci_dram_offset;* Note: I might drop the _ns suffix on the stream operations soon as it is * simply normal for stream operations to not swap in the first place. * +* Read operations have additional twi & isync to make sure the read +* is actually performed (i.e. the data has come back) before we start +* executing any following instructions. +* +* A data-dependent branch followed by an isync ensures that no +* instructions after the isync in program order will be +* (speculatively) executed before the isync has completed, and +* the isync won't complete until the branch is resolved. The +* the load that the twi depends on has to complete before +* anything else is executed; in particular, it's a barrier to +* keep MMIO reads ordered before main-storage accesses. */ #ifdef CONFIG_PPC64
Hello.
Quoted 22 lines · Linas Vepstas
Paul,
Please apply. This atch resulted from an email discussion back in Sept.--linas
Clarify why twi appears in the i/o macros.
Signed-off-by Linas Vepstas <[email protected]>Signed-off-by Segher Boessenkool <[email protected]>----
include/asm-powerpc/io.h | 11 +++++++++++ 1 file changed, 11 insertions(+)Index:
linux-2.6.19-git7/include/asm-powerpc/io.h
===================================================================include/asm-powerpc/io.h +0
-0@@ -78,6 +78,17 @@ extern unsigned long pci_dram_offset;* Note: I might drop the _ns suffix on the stream operations soon as it is * simply normal for stream operations to not swap in the first place.
include/asm-powerpc/io.h extern unsigned long pci_dram_offset; · line 89
Quoted 10 lines
include/asm-powerpc/io.h +9
-0* +* Read operations have additional twi & isync to make sure the read +* is actually performed (i.e. the data has come back) before we start +* executing any following instructions. +* +* A data-dependent branch followed by an isync ensures that no +* instructions after the isync in program order will be +* (speculatively) executed before the isync has completed, and +* the isync won't complete until the branch is resolved. The +* the load that the twi depends on has to complete before
A minor type here -- double "the".
include/asm-powerpc/io.h extern unsigned long pci_dram_offset; · line 94
Quoted 5 lines
include/asm-powerpc/io.h +2
-0+* anything else is executed; in particular, it's a barrier to +* keep MMIO reads ordered before main-storage accesses. */ #ifdef CONFIG_PPC64
WBR, Sergei
Paul, a revised patch below, please apply.
Quoted 2 lines · Sergei Shtylyov
A minor type here -- double "the".
Dohh. New patch below.
Quoted 1 line
WBR, Sergei
What does "WBR" mean?
--linas
Clarify why twi appears in the i/o macros.
----
include/asm-powerpc/io.h | 11 +++++++++++ 1 file changed, 11 insertions(+)
include/asm-powerpc/io.h +11 -0
@@ -78,6 +78,17 @@ extern unsigned long pci_dram_offset;* Note: I might drop the _ns suffix on the stream operations soon as it is * simply normal for stream operations to not swap in the first place. * +* Read operations have additional twi & isync to make sure the read +* is actually performed (i.e. the data has come back) before we start +* executing any following instructions. +* +* A data-dependent branch followed by an isync ensures that +* no instructions after the isync in program order will be +* (speculatively) executed before the isync has completed, +* and the isync won't complete until the branch is resolved. +* The load that the twi depends on has to complete before +* anything else is executed; in particular, it's a barrier +* to keep MMIO reads ordered before main-storage accesses. */ #ifdef CONFIG_PPC64
A message that is not in the archive (sent off-list or not yet received): [email protected]
Paul,
Please apply.
Quoted 2 lines · Sergei Shtylyov
Quoted 1 line
Quoted 1 line
A minor type here -- double "the".
And here as well. :-)
I forgot to say "quilt refresh" !? My quality control mechanism broke.
So again ...
--linas
Clarify why twi appears in the i/o macros. Shorten some over-length lines, while we're at it.
----
include/asm-powerpc/io.h | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-)
include/asm-powerpc/io.h +22 -11
@@ -62,22 +62,33 @@ extern unsigned long pci_dram_offset;* * Low level MMIO accessors * -* This provides the non-bus specific accessors to MMIO. Those are PowerPC -* specific and thus shouldn't be used in generic code. The accessors -* provided here are: +* This provides the non-bus specific accessors to MMIO. Those are +* PowerPC specific and thus shouldn't be used in generic code. The +* accessors provided here are: * * in_8, in_le16, in_be16, in_le32, in_be32, in_le64, in_be64 * out_8, out_le16, out_be16, out_le32, out_be32, out_le64, out_be64 * _insb, _insw_ns, _insl_ns, _outsb, _outsw_ns, _outsl_ns * -* Those operate directly on a kernel virtual address. Note that the prototype -* for the out_* accessors has the arguments in opposite order from the usual -* linux PCI accessors. Unlike those, they take the address first and the value -* next. -* -* Note: I might drop the _ns suffix on the stream operations soon as it is -* simply normal for stream operations to not swap in the first place. -* +* Those operate directly on a kernel virtual address. Note that the +* prototype for the out_* accessors has the arguments in opposite +* order from the usual linux PCI accessors. Unlike those, they take +* the address first and the value next. +* +* Note: I might drop the _ns suffix on the stream operations soon as it +* is simply normal for stream operations to not swap in the first place. +* +* Read operations have additional twi & isync to make sure the read +* is actually performed (i.e. the data has come back) before we start +* executing any following instructions. +* +* A data-dependent branch followed by an isync ensures that no +* instructions after the isync in program order will be +* (speculatively) executed before the isync has completed, and the +* isync won't complete until the branch is resolved. The load that +* the twi depends on has to complete before anything else is +* executed; in particular, it's a barrier to keep MMIO reads ordered +* before main-storage accesses. */ #ifdef CONFIG_PPC64
Quoted 2 lines · Linas Vepstas
Clarify why twi appears in the i/o macros. Shorten some over-length lines, while we're at it.
[snip]
Quoted 1 line
+ * A data-dependent branch followed by an isync ensures that no
I think it's potentially confusing to talk about data-dependent branches when what the code does is a twi instruction. Even if you argue that a twi is a data-dependent branch (and I disagree with that, since a trap is not a branch), that wouldn't be obvious to a casual reader of the code. In other words, I don't think the comment clarifies the situation very much.
Paul.
Quoted 6 lines
Quoted 1 line
+ * A data-dependent branch followed by an isync ensures that no
I think it's potentially confusing to talk about data-dependent branches when what the code does is a twi instruction. Even if you argue that a twi is a data-dependent branch (and I disagree with that, since a trap is not a branch),
If you argue it is *not* a branch, where in the architecture documentation can we find any language that gives us the guarantee we depend on here?
Quoted 2 lines
that wouldn't be obvious to a casual
reader of the code.
True enough.
Quoted 2 lines
In other words, I don't think the comment
clarifies the situation very much.
Got anything better? :-)
The first half of the new comment probably should go in no matter what (it says what twi;isync do here, not how exactly that works).
Segher
Quoted 10 lines · Segher Boessenkool
Quoted 6 lines
Quoted 1 line
+ * A data-dependent branch followed by an isync ensures that no
I think it's potentially confusing to talk about data-dependent branches when what the code does is a twi instruction. Even if you argue that a twi is a data-dependent branch (and I disagree with that, since a trap is not a branch),
If you argue it is *not* a branch, where in the architecture documentation can we find any language that gives us the guarantee we depend on here?
How about this wording: "A data-dependent branch, or trap word, followed by an isync ensures that ..."
Quoted 2 lines
Quoted 2 lines
that wouldn't be obvious to a casual
reader of the code.
I was trying to understand why the spidernet driver seemed to behave in the crazy ways that it does, and I decided to scrutinize this code. Even if one is relatively familiar with the overall powerpc architecture (as I am), the effect of the twi/isync was far from obvious, and left me scratching my head.
Quoted 2 lines
Quoted 2 lines
In other words, I don't think the comment
clarifies the situation very much.
Later on, I tripped over some text written by Segher, that made a lightbulb go off in my head ... and thus I was motiviated to plagiarizingly cut-n-paste it into here. So this block of text clarified things a lot for me, it "hit the spot".
Now, what I don't know is how accurate it really is ... I don't know if there aren't any strange deviations on 403 or 601 or any of the modern embedded processors, or whatever. But the text certainly makes me beleive I now know what's going on. As a service to the next person who scratches thier head in wonderment, we should have at least something written up here.
--linas
Quoted 3 lines · Segher Boessenkool
If you argue it is *not* a branch, where in the architecture documentation can we find any language that gives us the guarantee we depend on here?
isync is context synchronizing. In the definition of context synchronization, it says "2. The operation is not initiated, or in the case of isync, does not complete, until all instructions already in execution have completed to a point at which they have reported all exceptions they will cause". The twi conditionally causes an exception depending on the data from the previous load, therefore it cannot complete to a point at which is has reported all exceptions it will cause until it sees the data from the load. Therefore the isync cannot complete (and allow following instructions to start) until the data from the load has returned.
I once asked Ed Silha (one of the PowerPC architects) whether this reasoning was good, and he agreed it was.
Paul.
Quoted 6 lines · Paul Mackerras
Quoted 3 lines · Segher Boessenkool
If you argue it is *not* a branch, where in the architecture documentation can we find any language that gives us the guarantee we depend on here?
Quoted 10 lines
isync is context synchronizing. In the definition of context synchronization, it says "2. The operation is not initiated, or in the case of isync, does not complete, until all instructions already in execution have completed to a point at which they have reported all exceptions they will cause". The twi conditionally causes an exception depending on the data from the previous load, therefore it cannot complete to a point at which is has reported all exceptions it will cause until it sees the data from the load. Therefore the isync cannot complete (and allow following instructions to start) until the data from the load has returned.
I'd be happy if this text, verbatim or suitably massaged, were included in the header file.
--linas
Quoted 7 lines
isync is context synchronizing. In the definition of context synchronization, it says "2. The operation is not initiated, or in the case of isync, does not complete, until all instructions already in execution have completed to a point at which they have reported all exceptions they will cause".
The twi conditionally causes an exception
Except we use a trap with TO=0 so the CPU could optimise the dependency away -- or where does it say this is prohibited?
For conditional branches, this behaviour (not optimising away branches, even if they have a result independent on the data) is explicitly documented.
Quoted 8 lines
depending on the data from the previous load, therefore it cannot complete to a point at which is has reported all exceptions it will cause until it sees the data from the load. Therefore the isync cannot complete (and allow following instructions to start) until the data from the load has returned.
I once asked Ed Silha (one of the PowerPC architects) whether this reasoning was good, and he agreed it was.
My only nit is I can't find a guarantee that the trap instruction will have to get the read data as input before it can "report the exceptions it caused". Would you have anything on that? I'll sleep so much better if I know it's safe :-)
Segher
Quoted 2 lines · Segher Boessenkool
Except we use a trap with TO=0 so the CPU could optimise the dependency away -- or where does it say this is prohibited?
I don't know that it explicitly says it is prohibited, but Ed Silha agreed when I asked him that optimizing this would be against the spirit of the architecture, at least. I don't think the cpu designers would bother optimizing this since it wouldn't get them any extra performance on any benchmark.
Quoted 4 lines
My only nit is I can't find a guarantee that the trap instruction will have to get the read data as input before it can "report the exceptions it caused". Would you have anything on that? I'll sleep so much better if I know it's safe :-)
Well, clearly in general the conditional-trap instructions would have to see their input operand(s) before they can report an exception, since reporting an exception before that would be incorrect behaviour
- you would end up taking a 700 interrupt when you shouldn't. In other words I am asserting that if an instruction reports a trap exception, that leads inevitably to a 700 interrupt (in the architecture sense of "interrupt"), unless of course an earlier instruction also reports an exception.
Your point about TO=0 is reasonable, and something I also worried about a little, but Ed Silha didn't seem to think it was a problem.
Paul.
Quoted 6 lines
Quoted 2 lines
Except we use a trap with TO=0 so the CPU could optimise the dependency away -- or where does it say this is prohibited?
I don't know that it explicitly says it is prohibited, but Ed Silha agreed when I asked him that optimizing this would be against the spirit of the architecture, at least.
Yeah true. Weirder things that go against "the spirit of the architecture" have happened though :-)
Quoted 3 lines
I don't think the cpu designers
would bother optimizing this since it wouldn't get them any extra performance on any benchmark.
Not directly for this case -- but it might happen as a side effect of a more general optimisation perhaps. Who knows.
Quoted 9 lines
Quoted 4 lines
My only nit is I can't find a guarantee that the trap instruction will have to get the read data as input before it can "report the exceptions it caused". Would you have anything on that? I'll sleep so much better if I know it's safe :-)
Well, clearly in general the conditional-trap instructions would have to see their input operand(s) before they can report an exception, since reporting an exception before that would be incorrect behaviour
- you would end up taking a 700 interrupt when you shouldn't.
I meant for the case where the exceptions it reports are "no exceptions". In some cases (like TO=0) the CPU could short-circuit the evaluation of what exceptions can be caused before it has all the input data. As another (farfetched but perhaps more realistic) example, a CPU that does a lot of (memory) speculation could decide that an instruction will not cause an exception, if it doesn't do so on any of the possible (speculated) paths.
Quoted 2 lines
Your point about TO=0 is reasonable, and something I also worried about a little, but Ed Silha didn't seem to think it was a problem.
It would be lovely to get a guarantee about this in the architecture. Failing that, what about this comment (edited from Linas' patch and your mail):
Read operations have additional twi & isync to make sure the read is actually performed (i.e. the data has come back) before we start executing any following instructions.
isync is context synchronizing. In the definition of context synchronization, the architecture documents say "2. The operation is not initiated, or in the case of isync, does not complete, until all instructions already in execution have completed to a point at which they have reported all exceptions they will cause". The twi conditionally causes an exception depending on the data from the previous load; on all current CPUs it will not complete until it sees the data from the load. Therefore the isync cannot complete (and allow following instructions to start) until the data from the load has returned.
Segher