Skip to contentSkip to conversation

Fix MMIO ops to provide expected barrier behaviour

PATCH In review Paul Mackerras 2 participants 2 messages Sep 13, 2006 PowerPC

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix MMIO ops to provide expected barrier behaviour

This changes the writeX family of functions to have a sync instruction before the MMIO store rather than after, because the generally expected behaviour is that the device receiving the MMIO store can be guaranteed to see the effects of any preceding writes to normal memory.

To preserve ordering between writeX and readX, the readX family of functions have had an eieio added before the load.

Although writeX followed by spin_unlock is not officially guaranteed to keep the writeX inside the spin-locked region unless an mmiowb() is used, there are currently drivers that depend on the previous behaviour on powerpc, which was that the mmiowb wasn't actually required. Therefore we have a per-cpu flag that is set by writeX, cleared by __raw_spin_lock and mmiowb, and tested by __raw_spin_unlock. If it is set, __raw_spin_unlock does a sync and clears it.

Tested on G5 (PPC970) and POWER5.

---

arch/powerpc/kernel/misc.S +25 -25
index fc23040..6e95578 100644
@@ -74,11 +74,12 @@ _GLOBAL(_insb)
 	mtctr	r5
 	subi	r4,r4,1
 	blelr-
-00:	lbz	r5,0(r3)
-	eieio
+00:	eieio
+	lbz	r5,0(r3)
 	stbu	r5,1(r4)
 	bdnz	00b
-	IN_SYNC
+	twi	0,r5,0
+	isync
 	blr
 
 _GLOBAL(_outsb)
@@ -86,11 +87,10 @@ _GLOBAL(_outsb)
 	mtctr	r5
 	subi	r4,r4,1
 	blelr-
+	sync
 00:	lbzu	r5,1(r4)
 	stb	r5,0(r3)
-	EIEIO_32
 	bdnz	00b
-	SYNC_64
 	blr
 
 _GLOBAL(_insw)
@@ -98,11 +98,12 @@ _GLOBAL(_insw)
 	mtctr	r5
 	subi	r4,r4,2
 	blelr-
-00:	lhbrx	r5,0,r3
-	eieio
+00:	eieio
+	lhbrx	r5,0,r3
 	sthu	r5,2(r4)
 	bdnz	00b
-	IN_SYNC
+	twi	0,r5,0
+	isync
 	blr
 
 _GLOBAL(_outsw)
@@ -110,11 +111,10 @@ _GLOBAL(_outsw)
 	mtctr	r5
 	subi	r4,r4,2
 	blelr-
+	sync
 00:	lhzu	r5,2(r4)
-	EIEIO_32
 	sthbrx	r5,0,r3
 	bdnz	00b
-	SYNC_64
 	blr
 
 _GLOBAL(_insl)
@@ -122,11 +122,12 @@ _GLOBAL(_insl)
 	mtctr	r5
 	subi	r4,r4,4
 	blelr-
-00:	lwbrx	r5,0,r3
-	eieio
+00:	eieio
+	lwbrx	r5,0,r3
 	stwu	r5,4(r4)
 	bdnz	00b
-	IN_SYNC
+	twi	0,r5,0
+	isync
 	blr
 
 _GLOBAL(_outsl)
@@ -134,11 +135,10 @@ _GLOBAL(_outsl)
 	mtctr	r5
 	subi	r4,r4,4
 	blelr-
+	sync
 00:	lwzu	r5,4(r4)
 	stwbrx	r5,0,r3
-	EIEIO_32
 	bdnz	00b
-	SYNC_64
 	blr
 
 #ifdef CONFIG_PPC32
@@ -149,11 +149,12 @@ _GLOBAL(_insw_ns)
 	mtctr	r5
 	subi	r4,r4,2
 	blelr-
-00:	lhz	r5,0(r3)
-	eieio
+00:	eieio
+	lhz	r5,0(r3)
 	sthu	r5,2(r4)
 	bdnz	00b
-	IN_SYNC
+	twi	0,r5,0
+	isync
 	blr
 
 #ifdef CONFIG_PPC32
@@ -164,11 +165,10 @@ _GLOBAL(_outsw_ns)
 	mtctr	r5
 	subi	r4,r4,2
 	blelr-
+	sync
 00:	lhzu	r5,2(r4)
 	sth	r5,0(r3)
-	EIEIO_32
 	bdnz	00b
-	SYNC_64
 	blr
 
 #ifdef CONFIG_PPC32
@@ -179,11 +179,12 @@ _GLOBAL(_insl_ns)
 	mtctr	r5
 	subi	r4,r4,4
 	blelr-
-00:	lwz	r5,0(r3)
-	eieio
+00:	eieio
+	lwz	r5,0(r3)
 	stwu	r5,4(r4)
 	bdnz	00b
-	IN_SYNC
+	twi	0,r5,0
+	isync
 	blr
 
 #ifdef CONFIG_PPC32
@@ -194,10 +195,9 @@ _GLOBAL(_outsl_ns)
 	mtctr	r5
 	subi	r4,r4,4
 	blelr-
+	sync
 00:	lwzu	r5,4(r4)
 	stw	r5,0(r3)
-	EIEIO_32
 	bdnz	00b
-	SYNC_64
 	blr
 
include/asm-powerpc/eeh.h +1 -0
index 4df3e80..c8f0a94 100644
@@ -229,6 +229,7 @@ static inline void eeh_memcpy_fromio(voi
 	void *destsave = dest;
 	unsigned long nsave = n;
 
+	__asm__ __volatile__ ("eieio" : : : "memory");
 	while(n && (!EEH_CHECK_ALIGN(vsrc, 4) || !EEH_CHECK_ALIGN(dest, 4))) {
 		*((u8 *)dest) = *((volatile u8 *)vsrc);
 		__asm__ __volatile__ ("eieio" : : : "memory");
include/asm-powerpc/io.h +28 -15

139 lines not on this page · raw message

include/asm-powerpc/paca.h +1 -0
index 2d4585f..3d5d590 100644
@@ -93,6 +93,7 @@ #endif /* CONFIG_PPC_ISERIES */
 	u64 saved_r1;			/* r1 save for RTAS calls */
 	u64 saved_msr;			/* MSR saved here by enter_rtas */
 	u8 proc_enabled;		/* irq soft-enable flag */
+	u8 io_sync;			/* writel() needs spin_unlock sync */
 
 	/* Stuff for accurate time accounting */
 	u64 user_time;			/* accumulated usermode TB ticks */
include/asm-powerpc/spinlock.h +17 -0
index 895cb6d..c31e438 100644
@@ -36,6 +36,19 @@ #else
 #define LOCK_TOKEN	1
 #endif
 
+#if defined(CONFIG_PPC64) && defined(CONFIG_SMP)
+#define CLEAR_IO_SYNC	(get_paca()->io_sync = 0)
+#define SYNC_IO		do {						\
+				if (unlikely(get_paca()->io_sync)) {	\
+					mb();				\
+					get_paca()->io_sync = 0;	\
+				}					\
+			} while (0)
+#else
+#define CLEAR_IO_SYNC
+#define SYNC_IO
+#endif
+
 /*
  * This returns the old value in the lock, so we succeeded
  * in getting the lock if the return value is 0.
@@ -61,6 +74,7 @@ static __inline__ unsigned long __spin_t
 
 static int __inline__ __raw_spin_trylock(raw_spinlock_t *lock)
 {
+	CLEAR_IO_SYNC;
 	return __spin_trylock(lock) == 0;
 }
 
@@ -91,6 +105,7 @@ #endif
 
 static void __inline__ __raw_spin_lock(raw_spinlock_t *lock)
 {
+	CLEAR_IO_SYNC;
 	while (1) {
 		if (likely(__spin_trylock(lock) == 0))
 			break;
@@ -107,6 +122,7 @@ static void __inline__ __raw_spin_lock_f
 {
 	unsigned long flags_dis;
 
+	CLEAR_IO_SYNC;
 	while (1) {
 		if (likely(__spin_trylock(lock) == 0))
 			break;
@@ -124,6 +140,7 @@ static void __inline__ __raw_spin_lock_f
 
 static __inline__ void __raw_spin_unlock(raw_spinlock_t *lock)
 {
+	SYNC_IO;
 	__asm__ __volatile__("# __raw_spin_unlock\n\t"
 				LWSYNC_ON_SMP: : :"memory");
 	lock->slock = 0;
#1 lore raw
Quoted 4 lines

This changes the writeX family of functions to have a sync instruction before the MMIO store rather than after, because the generally expected behaviour is that the device receiving the MMIO store can be guaranteed to see the effects of any preceding writes to normal memory.

Yay, progress!

Quoted 2 lines

To preserve ordering between writeX and readX, the readX family of functions have had an eieio added before the load.

readX() is supposed to be ordered to memory as well; the only example I can think of where the difference would would show is a readX() setting off a DMA; maybe such devices do not exist anyway, but if you care, the eieio should be a full sync.

Quoted 7 lines

Although writeX followed by spin_unlock is not officially guaranteed to keep the writeX inside the spin-locked region unless an mmiowb() is used, there are currently drivers that depend on the previous behaviour on powerpc, which was that the mmiowb wasn't actually required. Therefore we have a per-cpu flag that is set by writeX, cleared by __raw_spin_lock and mmiowb, and tested by __raw_spin_unlock. If it is set, __raw_spin_unlock does a sync and clears it.

Why is this done on 64-bit systems only?

Segher