Skip to contentSkip to conversation

question on howto use invalidate_dcache_range

DISCUSSION Stephan Linke 4 participants 12 messages Mar 12, 2003 – Mar 13, 2003

lore
Share & tools
lore
#0 lore raw

Hi,

in November there was the discussion about Joakims patch to 8xx_io/enet.c.
(http://lists.linuxppc.org/linuxppc-embedded/200211/msg00122.html)

Since the result of the discussion didn't seam that clear to me I still have a question related to this.

After all is it realy neccessary to use invalidate_dcache range before giving the transmit buffer to the DMA?
That's one of Joakims last modifications but what exactly is the reason? It's obvious for the flush command but I couldn't figure
out the reason for invalidate.
Beside the cache line alignment problem (causing nearby data to be flushed or invalidated) are there any other problems to be taken care of?

Thanks, Stephan

** Sent via the linuxppc-embedded mail list. See http://lists.linuxppc.org/

#1 lore raw
Quoted 6 lines

Hi,

in November there was the discussion about Joakims patch to 8xx_io/enet.c.
(http://lists.linuxppc.org/linuxppc-embedded/200211/msg00122.html)

Quoted 6 lines

Since the result of the discussion didn't seam that clear to me I still have a question related to this.

After all is it realy neccessary to use invalidate_dcache range before giving the transmit buffer to the DMA?
That's one of Joakims last modifications but what exactly is the reason? It's obvious for the flush command but I
couldn't figure
out the reason for invalidate.

Yes, it is neccessary. When you allocate a new skb buffer it you may get a recently used buffer. The TCP/UPD/IP layer(s) may have modified some of the contents in that buffer before freeing it. Therefore can the dcache contain dirty(modified) cache lines which belong to the buffer. When the CPU needs more dcache, it will flush those dirty cache lines to memory which now belong to the CPM. By invalidating the buffer you tell the CPU to free those cachelines whithout writeing them to memory.

Quoted 3 lines

Beside the cache line alignment problem (causing nearby data to be flushed or invalidated) are there any other problems to be taken
care of?

There are no cache line alignment problems. There was some concern(expressed by Dan) that some vital data at the end of the skb buffer could be invalidated due to bad aligment. See http://lists.linuxppc.org/linuxppc-embedded/200302/msg00135.html
These has been fixed in the skb layer long ago.

The patch has been around for many months now and I have not received any problem reports.

Jocke

Quoted 1 line

Thanks, Stephan

** Sent via the linuxppc-embedded mail list. See http://lists.linuxppc.org/

#2 lore raw

Hi Joke,

thanks for your explaination. Verry plausible to me now.
I guess we have to take care for the alignment anyway since we don't use sk buffer for some reason :).

Thanks, Stephan

Quoted 35 lines

-----Original Message-----
From: Joakim Tjernlund [mailto:[email protected]]
Sent: Donnerstag, 13. März 2003 08:41
To: Stephan Linke; Linuxppc-Embedded
Subject: RE: question on howto use invalidate_dcache_range

Quoted 11 lines

Hi,

in November there was the discussion about Joakims patch to 8xx_io/enet.c.
(http://lists.linuxppc.org/linuxppc-embedded/200211/msg00122.html)

Since the result of the discussion didn't seam that clear to me I still have a question related to this.

After all is it realy neccessary to use invalidate_dcache range before giving the transmit buffer to the DMA?
That's one of Joakims last modifications but what exactly is the reason? It's obvious for the flush command but I
couldn't figure
out the reason for invalidate.

Yes, it is neccessary. When you allocate a new skb buffer it you may get a recently used buffer. The TCP/UPD/IP layer(s) may have modified some of the contents in that buffer before freeing it. Therefore can the dcache contain dirty(modified) cache lines which belong to the buffer. When the CPU needs more dcache, it will flush those dirty cache lines to memory which now belong to the CPM. By invalidating the buffer you tell the CPU to free those cachelines whithout writeing them to memory.

Quoted 3 lines

Beside the cache line alignment problem (causing nearby data to be flushed or invalidated) are there any other problems to be taken
care of?

There are no cache line alignment problems. There was some concern(expressed by Dan) that some vital data at the end of the skb buffer could be invalidated due to bad aligment. See http://lists.linuxppc.org/linuxppc-embedded/200302/msg00135.html
These has been fixed in the skb layer long ago.

Quoted 9 lines

The patch has been around for many months now and I have not received any problem reports.

Jocke

Quoted 1 line

Thanks, Stephan

** Sent via the linuxppc-embedded mail list. See http://lists.linuxppc.org/

#3 lore raw
Quoted 4 lines

Hi Joke,

thanks for your explaination. Verry plausible to me now.

:-)

Quoted 2 lines

I guess we have to take care for the alignment anyway since we don't use sk buffer for some reason :).

What do you mean? Do have a custom driver which uses some other buffer mgmt?

Jocke

** Sent via the linuxppc-embedded mail list. See http://lists.linuxppc.org/

#4 lore raw

Hi Joke,

Quoted 4 lines

What do you mean? Do have a custom driver which uses some other buffer mgmt?

That's it.

Thanks, Stephan

** Sent via the linuxppc-embedded mail list. See http://lists.linuxppc.org/

#5 lore raw
In reply to Stephan Linke
Quoted 1 line · Stephan Linke

After all is it realy neccessary to use invalidate_dcache range before giving the transmit buffer to the DMA?

The logically correct cache operation is to invalidate an incoming buffer after the DMA has completed, and to (obviously) push an outgoing buffer prior to starting the DMA.

	-- Dan

** Sent via the linuxppc-embedded mail list. See http://lists.linuxppc.org/

#6 lore raw
Quoted 8 lines
Quoted 1 line · Stephan Linke

After all is it realy neccessary to use invalidate_dcache range before giving the transmit buffer to the DMA?

The logically correct cache operation is to invalidate an incoming buffer after the DMA has completed

Yes, but you still need to invalidate the buffer before also or invalidate it when the buffer is freed which may be possible if you are using your own buffer mgmt.

** Sent via the linuxppc-embedded mail list. See http://lists.linuxppc.org/

#7 lore raw
Quoted 2 lines · Joakim Tjernlund

Yes, but you still need to invalidate the buffer before also or invalidate it when the buffer is freed which may be possible if you are using your own buffer mgmt.

Why?

	-- Dan

** Sent via the linuxppc-embedded mail list. See http://lists.linuxppc.org/

#8 lore raw

Joakim> Yes, but you still need to invalidate the buffer before Joakim> also or invalidate it when the buffer is freed which may Joakim> be possible if you are using your own buffer mgmt.

Dan> Why?

Is the following not possible:

CPU has a dirty cache line inside the buffer
Driver tells device to start DMA
Device finishes DMA
CPU writes back dirty cache line
Driver gets notification that DMA completed
Driver invalidates cache for buffer

The invalidate above is too late: the data is already corrupted by the write back of the dirty cache.

- Roland

** Sent via the linuxppc-embedded mail list. See http://lists.linuxppc.org/

#9 lore raw
Quoted 1 line · Roland Dreier

Is the following not possible:

That's a problem in any system design. Consider what would happen if you had an architecture that was hardware cache coherent. Software that writes to buffers at any time that is also a DMA target will result in unpredictable behavior. The system design for what you described requires a higher level of shared memory software synchronization as it is exactly the unpredictability that has to be prevented in any SMP design.

What you described is a real problem with cache unaligned buffers that has to be avoided in systems that do not have hardware cache coherency.

	-- Dan

** Sent via the linuxppc-embedded mail list. See http://lists.linuxppc.org/

#10 lore raw

Dan> That's a problem in any system design. Consider what would Dan> happen if you had an architecture that was hardware cache Dan> coherent. Software that writes to buffers at any time that Dan> is also a DMA target will result in unpredictable behavior. Dan> The system design for what you described requires a higher Dan> level of shared memory software synchronization as it is Dan> exactly the unpredictability that has to be prevented in any Dan> SMP design.

I think you misunderstood. The dirty cache line could be produced by a write to memory executed long before the DMA is initiated. In a hardware cache coherent architecture, the bus controller would just snoop the CPU's cache and invalidate the dirty cache line.

In any case the current Linux kernel seems to do things the way I believe is necessary: pci_map_single(...PCI_DMA_FROMDEVICE) invalidates the cache (and is done _before_ the DMA is initiated), while pci_unmap_single() is a NOOP.

- Roland

** Sent via the linuxppc-embedded mail list. See http://lists.linuxppc.org/