powerpc: add the missing required isync for the coherent icache flush
PATCH 1/1 powerpc: add the missing required isync for the coherent icache flush
Even we don't need to flush the dcache and invalidate the icache on the CPU which has coherent icache. But we do need an isync to discard the prefetched instructions in this case.
--- arch/powerpc/kernel/misc_32.S | 2 ++ arch/powerpc/kernel/misc_64.S | 1 + 2 files changed, 3 insertions(+)
arch/powerpc/kernel/misc_32.S +2 -0
@@ -433,6 +433,7 @@ _GLOBAL(invalidate_dcache_range) */ _GLOBAL(__flush_dcache_icache) BEGIN_FTR_SECTION + isync blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) rlwinm r3,r3,0,0,31-PAGE_SHIFT /* Get page base address */
@@ -474,6 +475,7 @@ END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_44x) */ _GLOBAL(__flush_dcache_icache_phys) BEGIN_FTR_SECTION + isync blr /* for 601, do nothing */ END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) mfmsr r10
arch/powerpc/kernel/misc_64.S +1 -0
@@ -69,6 +69,7 @@ PPC64_CACHES: _KPROBE(flush_icache_range) BEGIN_FTR_SECTION + isync blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) /*
Signature · 1 line
1.8.3.1
Quoted 27 lines
-----Original Message-----
From: Linuxppc-dev [mailto:linuxppc-dev- [email protected]] On Behalf Of Kevin Hao
Sent: Thursday, August 15, 2013 7:56 PM
To: Benjamin Herrenschmidt
Cc: linuxppc
Subject: [PATCH] powerpc: add the missing required isync for the coherent icache flush
=20
Even we don't need to flush the dcache and invalidate the icache on the CPU which has coherent icache. But we do need an isync to discard the prefetched instructions in this case.
=20Signed-off-by Kevin Hao <[email protected]>--- arch/powerpc/kernel/misc_32.S | 2 ++ arch/powerpc/kernel/misc_64.S | 1 + 2 files changed, 3 insertions(+)=20
arch/powerpc/kernel/misc_32.S +0
-0
arch/powerpc/kernel/misc_32.S _GLOBAL(invalidate_dcache_range) · line 436
Quoted 4 lines
arch/powerpc/kernel/misc_32.S +1
-0@@ -433,6 +433,7 @@ _GLOBAL(invalidate_dcache_range) */ _GLOBAL(__flush_dcache_icache) BEGIN_FTR_SECTION + isync
There is not touch the icache, why need we to do this?
Quoted 32 lines
arch/powerpc/kernel/misc_32.S +0
-0blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) rlwinm r3,r3,0,0,31-PAGE_SHIFT /* Get page base address*/
arch/powerpc/kernel/misc_32.S +1
-0@@ -474,6 +475,7 @@ END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_44x) */ _GLOBAL(__flush_dcache_icache_phys) BEGIN_FTR_SECTION + isync blr /* for 601, do nothing */ END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) mfmsr r10arch/powerpc/kernel/misc_64.S +0
-0@@ -69,6 +69,7 @@ PPC64_CACHES:=20
_KPROBE(flush_icache_range)
BEGIN_FTR_SECTIONarch/powerpc/kernel/misc_64.S +1
-1+ isync blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) /* --1.8.3.1
=20
_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev
Quoted 20 lines · On Mon, 2013-08-19 at 03:24 +0000, Wang Dongsheng-B40534 wrote:
Quoted 20 lines
-----Original Message-----
From: Linuxppc-dev [mailto:linuxppc-dev- [email protected]] On Behalf Of Kevin Hao
Sent: Thursday, August 15, 2013 7:56 PM
To: Benjamin Herrenschmidt
Cc: linuxppc
Subject: [PATCH] powerpc: add the missing required isync for the coherent icache flushEven we don't need to flush the dcache and invalidate the icache on the CPU which has coherent icache. But we do need an isync to discard the prefetched instructions in this case.
Signed-off-by Kevin Hao <[email protected]>--- arch/powerpc/kernel/misc_32.S | 2 ++ arch/powerpc/kernel/misc_64.S | 1 + 2 files changed, 3 insertions(+)
Quoted 11 lines
arch/powerpc/kernel/misc_32.S _GLOBAL(invalidate_dcache_range) · line 436
Quoted 10 lines
arch/powerpc/kernel/misc_32.S +1
-0@@ -433,6 +433,7 @@ _GLOBAL(invalidate_dcache_range) */ _GLOBAL(__flush_dcache_icache) BEGIN_FTR_SECTION + isyncThere is not touch the icache, why need we to do this?
The semantic of this function is to make data executable. Even if the implementation has a snooping icache, it *still* needs to make sure prefetched code is tossed out of the pipeline which is what isync should provide.
The architecture actually specifies that.
Cheers,
Ben.
Quoted 33 lines
Quoted 32 lines
arch/powerpc/kernel/misc_32.S +0
-0blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) rlwinm r3,r3,0,0,31-PAGE_SHIFT /* Get page base address*/
arch/powerpc/kernel/misc_32.S +1
-0@@ -474,6 +475,7 @@ END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_44x) */ _GLOBAL(__flush_dcache_icache_phys) BEGIN_FTR_SECTION + isync blr /* for 601, do nothing */ END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) mfmsr r10arch/powerpc/kernel/misc_64.S +1
-1@@ -69,6 +69,7 @@ PPC64_CACHES: _KPROBE(flush_icache_range) BEGIN_FTR_SECTION + isync blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) /* --1.8.3.1
_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev
Quoted 6 lines · Benjamin Herrenschmidt
The semantic of this function is to make data executable. Even if the implementation has a snooping icache, it *still* needs to make sure prefetched code is tossed out of the pipeline which is what isync should provide.
The architecture actually specifies that.
In fact, on P5 and later, I think we are supposed to do a single dummy icbi followed by sync and isync.
Cheers,
Ben.
Quoted 37 lines
Cheers,
Ben.Quoted 33 lines
Quoted 32 lines
arch/powerpc/kernel/misc_32.S +0
-0blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) rlwinm r3,r3,0,0,31-PAGE_SHIFT /* Get page base address*/
arch/powerpc/kernel/misc_32.S +1
-0@@ -474,6 +475,7 @@ END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_44x) */ _GLOBAL(__flush_dcache_icache_phys) BEGIN_FTR_SECTION + isync blr /* for 601, do nothing */ END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) mfmsr r10arch/powerpc/kernel/misc_64.S +1
-1@@ -69,6 +69,7 @@ PPC64_CACHES: _KPROBE(flush_icache_range) BEGIN_FTR_SECTION + isync blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) /* --1.8.3.1
_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev
Quoted 11 lines · Benjamin Herrenschmidt
Quoted 6 lines · Benjamin Herrenschmidt
The semantic of this function is to make data executable. Even if the implementation has a snooping icache, it *still* needs to make sure prefetched code is tossed out of the pipeline which is what isync should provide.
The architecture actually specifies that.
In fact, on P5 and later, I think we are supposed to do a single dummy icbi followed by sync and isync.
Do you mean something like this? But why?
arch/powerpc/kernel/misc_64.S +4 -0
@@ -69,6 +69,8 @@ PPC64_CACHES: _KPROBE(flush_icache_range) BEGIN_FTR_SECTION + icbi 0,r3 + sync isync blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)
@@ -209,6 +211,8 @@ _GLOBAL(flush_inval_dcache_range) */ _GLOBAL(__flush_dcache_icache) BEGIN_FTR_SECTION + icbi 0,r3 + sync isync blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)
Thanks,
Kevin
Quoted 43 lines
Cheers,
Ben.Quoted 37 lines
Cheers,
Ben.Quoted 33 lines
Quoted 32 lines
(quoted diff) +0
-0blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) rlwinm r3,r3,0,0,31-PAGE_SHIFT /* Get page base address*/
(quoted diff) +1
-0@@ -474,6 +475,7 @@ END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_44x) */ _GLOBAL(__flush_dcache_icache_phys) BEGIN_FTR_SECTION + isync blr /* for 601, do nothing */ END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) mfmsr r10arch/powerpc/kernel/misc_64.S +1
-1@@ -69,6 +69,7 @@ PPC64_CACHES: _KPROBE(flush_icache_range) BEGIN_FTR_SECTION + isync blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) /* --1.8.3.1
_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev
Quoted 8 lines · Kevin Hao
Quoted 7 lines · Benjamin Herrenschmidt
Quoted 5 lines · Benjamin Herrenschmidt
The semantic of this function is to make data executable. Even if the implementation has a snooping icache, it *still* needs to make sure prefetched code is tossed out of the pipeline which is what isync should provide.
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
The architecture actually specifies that.
In fact, on P5 and later, I think we are supposed to do a single dummy icbi followed by sync and isync.
Do you mean something like this? But why?
It has to do with making sure prefetched and/or speculated instructions are properly tossed.
My understanding is that icbi basically tells the ifetch buffers to drop it, sync orders the icbi and isync ensures its execution has been synchronized. At least I *think* that's the required sequence, I have to dbl check the arch, maybe tomorrow. I wouldn't be surprise if we also need a sync before the icbi to order the actual stores to memory that might have modified instructions with the icbi.
Cheers,
Ben.
Quoted 69 lines
arch/powerpc/kernel/misc_64.S +4
-0@@ -69,6 +69,8 @@ PPC64_CACHES: _KPROBE(flush_icache_range) BEGIN_FTR_SECTION + icbi 0,r3 + sync isync blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)@@ -209,6 +211,8 @@ _GLOBAL(flush_inval_dcache_range) */ _GLOBAL(__flush_dcache_icache) BEGIN_FTR_SECTION + icbi 0,r3 + sync isync blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)Thanks,
KevinQuoted 43 lines
Cheers,
Ben.Quoted 37 lines
Cheers,
Ben.Quoted 33 lines
Quoted 32 lines
arch/powerpc/kernel/misc_64.S +0
-0blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) rlwinm r3,r3,0,0,31-PAGE_SHIFT /* Get page base address*/
(quoted diff) +1
-0@@ -474,6 +475,7 @@ END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_44x) */ _GLOBAL(__flush_dcache_icache_phys) BEGIN_FTR_SECTION + isync blr /* for 601, do nothing */ END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) mfmsr r10arch/powerpc/kernel/misc_64.S +1
-1@@ -69,6 +69,7 @@ PPC64_CACHES: _KPROBE(flush_icache_range) BEGIN_FTR_SECTION + isync blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) /* --1.8.3.1
_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev
Quoted 14 lines · Benjamin Herrenschmidt
Quoted 13 lines · Kevin Hao
Quoted 11 lines · Benjamin Herrenschmidt
Quoted 6 lines · Benjamin Herrenschmidt
The semantic of this function is to make data executable. Even if the implementation has a snooping icache, it *still* needs to make sure prefetched code is tossed out of the pipeline which is what isync should provide.
The architecture actually specifies that.
In fact, on P5 and later, I think we are supposed to do a single dummy icbi followed by sync and isync.
Quoted 7 lines
Quoted 1 line
Do you mean something like this? But why?
It has to do with making sure prefetched and/or speculated instructions are properly tossed.
My understanding is that icbi basically tells the ifetch buffers to drop it
Dummy question: What does the ifetch buffers mean? The instruction fetch pipeline or instruction dispatch pipeline? Shouldn't all the prefetched instructions in these buffers be discarded by isync?
Quoted 5 lines
, sync orders the icbi and isync ensures its execution has been synchronized. At least I *think* that's the required sequence, I have to dbl check the arch, maybe tomorrow. I wouldn't be surprise if we also need a sync before the icbi to order the actual stores to memory that might have modified instructions with the icbi.
Doesn't the coherence between icache and dcache be maintained by the snooping?
Thanks,
Kevin
Quoted 75 lines
Cheers,
Ben.Quoted 69 lines
arch/powerpc/kernel/misc_64.S +4
-0@@ -69,6 +69,8 @@ PPC64_CACHES: _KPROBE(flush_icache_range) BEGIN_FTR_SECTION + icbi 0,r3 + sync isync blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)@@ -209,6 +211,8 @@ _GLOBAL(flush_inval_dcache_range) */ _GLOBAL(__flush_dcache_icache) BEGIN_FTR_SECTION + icbi 0,r3 + sync isync blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)Thanks,
KevinQuoted 43 lines
Cheers,
Ben.Quoted 37 lines
Cheers,
Ben.Quoted 33 lines
Quoted 32 lines
arch/powerpc/kernel/misc_64.S +0
-0blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) rlwinm r3,r3,0,0,31-PAGE_SHIFT /* Get page base address*/
(quoted diff) +1
-0@@ -474,6 +475,7 @@ END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_44x) */ _GLOBAL(__flush_dcache_icache_phys) BEGIN_FTR_SECTION + isync blr /* for 601, do nothing */ END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) mfmsr r10arch/powerpc/kernel/misc_64.S +1
-1@@ -69,6 +69,7 @@ PPC64_CACHES: _KPROBE(flush_icache_range) BEGIN_FTR_SECTION + isync blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) /* --1.8.3.1
_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev
Quoted 3 lines · Kevin Hao
Dummy question: What does the ifetch buffers mean? The instruction fetch pipeline or instruction dispatch pipeline? Shouldn't all the prefetched instructions in these buffers be discarded by isync?
Architecturally isync doesn't have to toss prefetch completely. It only needs to make sense that context changes performed by previous instructions (and interrupts/traps) happen at the point of the isync, for example, it will ensure that a trap conditional is fully evaluated before subsequent instruction execution, etc....
So in this case, it makes sure the icbi has been executed and it's the icbi that invalidates the prefetched instructions.
Quoted 8 lines
Quoted 6 lines
, sync orders the icbi and isync ensures its execution has been synchronized. At least I *think* that's the required sequence, I have to dbl check the arch, maybe tomorrow. I wouldn't be surprise if we also need a sync before the icbi to order the actual stores to memory that might have modified instructions with the icbi.
Doesn't the coherence between icache and dcache be maintained by the snooping?
The icache yes, but not the ifetch buffers (basically think of them as buffering stages between icache and dispatch). At least that's my understanding of the implementation.
Cheers,
Ben.
Quoted 78 lines
Thanks,
KevinQuoted 75 lines
Cheers,
Ben.Quoted 69 lines
arch/powerpc/kernel/misc_64.S +4
-0@@ -69,6 +69,8 @@ PPC64_CACHES: _KPROBE(flush_icache_range) BEGIN_FTR_SECTION + icbi 0,r3 + sync isync blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)@@ -209,6 +211,8 @@ _GLOBAL(flush_inval_dcache_range) */ _GLOBAL(__flush_dcache_icache) BEGIN_FTR_SECTION + icbi 0,r3 + sync isync blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)Thanks,
KevinQuoted 43 lines
Cheers,
Ben.Quoted 37 lines
Cheers,
Ben.Quoted 33 lines
Quoted 32 lines
arch/powerpc/kernel/misc_64.S +0
-0blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) rlwinm r3,r3,0,0,31-PAGE_SHIFT /* Get page base address*/
(quoted diff) +1
-0@@ -474,6 +475,7 @@ END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_44x) */ _GLOBAL(__flush_dcache_icache_phys) BEGIN_FTR_SECTION + isync blr /* for 601, do nothing */ END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) mfmsr r10arch/powerpc/kernel/misc_64.S +1
-1@@ -69,6 +69,7 @@ PPC64_CACHES: _KPROBE(flush_icache_range) BEGIN_FTR_SECTION + isync blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) /* --1.8.3.1
_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev
Quoted 22 lines · Benjamin Herrenschmidt
Quoted 3 lines · Kevin Hao
Dummy question: What does the ifetch buffers mean? The instruction fetch pipeline or instruction dispatch pipeline? Shouldn't all the prefetched instructions in these buffers be discarded by isync?
Architecturally isync doesn't have to toss prefetch completely. It only needs to make sense that context changes performed by previous instructions (and interrupts/traps) happen at the point of the isync, for example, it will ensure that a trap conditional is fully evaluated before subsequent instruction execution, etc....
So in this case, it makes sure the icbi has been executed and it's the icbi that invalidates the prefetched instructions.
Quoted 7 lines
Quoted 6 lines
, sync orders the icbi and isync ensures its execution has been synchronized. At least I *think* that's the required sequence, I have to dbl check the arch, maybe tomorrow. I wouldn't be surprise if we also need a sync before the icbi to order the actual stores to memory that might have modified instructions with the icbi.
Quoted 5 lines
Quoted 1 line
Doesn't the coherence between icache and dcache be maintained by the snooping?
The icache yes, but not the ifetch buffers (basically think of them as buffering stages between icache and dispatch). At least that's my understanding of the implementation.
OK. Thanks for the explanation. I will update the patch according to your suggestions.
Thanks,
Kevin
PATCH v2 1/1 powerpc: purge all the prefetched instructions for the coherent icache flush
As Benjamin Herrenschmidt has indicated, we still need a dummy icbi to purge all the prefetched instructions from the ifetch buffers for the snooping icache. We also need a sync before the icbi to order the actual stores to memory that might have modified instructions with the icbi.
--- arch/powerpc/include/asm/cache.h | 14 +++++++++++++- arch/powerpc/kernel/misc_32.S | 4 +++- arch/powerpc/kernel/misc_64.S | 3 ++- 3 files changed, 18 insertions(+), 3 deletions(-)
arch/powerpc/include/asm/cache.h +13 -1
@@ -41,8 +41,20 @@ struct ppc64_caches {extern struct ppc64_caches ppc64_caches;#endif /* __powerpc64__ && ! __ASSEMBLY__ */ -#if !defined(__ASSEMBLY__) +#if defined(__ASSEMBLY__) +/* +* For a snooping icache, we still need a dummy icbi to purge all the +* prefetched instructions from the ifetch buffers. We also need a sync +* before the icbi to order the the actual stores to memory that might +* have modified instructions with the icbi. +*/ +#define PURGE_PREFETCHED_INS \ + sync; \ + icbi 0,r3; \ + sync; \ + isync +#else #define __read_mostly __attribute__((__section__(".data..read_mostly")))#ifdef CONFIG_6xx
arch/powerpc/kernel/misc_32.S +3 -1
@@ -329,7 +329,7 @@ END_FTR_SECTION_IFSET(CPU_FTR_UNIFIED_ID_CACHE) */ _KPROBE(flush_icache_range) BEGIN_FTR_SECTION - isync + PURGE_PREFETCHED_INS blr /* for 601, do nothing */ END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) li r5,L1_CACHE_BYTES-1
@@ -433,6 +433,7 @@ _GLOBAL(invalidate_dcache_range) */ _GLOBAL(__flush_dcache_icache) BEGIN_FTR_SECTION + PURGE_PREFETCHED_INS blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) rlwinm r3,r3,0,0,31-PAGE_SHIFT /* Get page base address */
@@ -474,6 +475,7 @@ END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_44x) */ _GLOBAL(__flush_dcache_icache_phys) BEGIN_FTR_SECTION + PURGE_PREFETCHED_INS blr /* for 601, do nothing */ END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) mfmsr r10
arch/powerpc/kernel/misc_64.S +2 -1
@@ -69,6 +69,7 @@ PPC64_CACHES: _KPROBE(flush_icache_range) BEGIN_FTR_SECTION + PURGE_PREFETCHED_INS blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) /*
@@ -208,7 +209,7 @@ _GLOBAL(flush_inval_dcache_range) */ _GLOBAL(__flush_dcache_icache) BEGIN_FTR_SECTION - isync + PURGE_PREFETCHED_INS blr END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE) /*
Signature · 1 line
1.8.3.1