Q: volatile vs barriers to access memory data changed by device DMA

From: Eliot Blennerhassett
Date: Sun Feb 24 2008 - 20:15:34 EST


Greetings,

Currently I have a driver that uses "volatile", which I want to remove.
As others have said "volatile is useless"
Heres the relevant source.
http://hg.alsa-project.org/alsa-driver/file/89222d702376/pci/asihpi/hpi6205.c

There's quite a bit written about barriers, but most seems to be assuming SMP
situation or memory mapped devices. Not much about devices doing DMA.
I.e I have read Documentation/memory-barriers.txt, and some of the threads in
lkml, but still am unsure.

The "volatile" is applied to structures that are either read or written by
device DMA. ÂCertainly the driver in its current state doesn't work without
volatile qualifier. (BTW the device doesn't use host interrupts)

Now, I want to get rid of the volatile, and replace it with ?some kind of
barrier?

In the following, am I using the barriers correctly?

Note that structures ("interface") used for dma are allocated with
dma_alloc_coherent()

1) Reading something updated by DMA
Here the volatile or barrier is needed or the loop gets optimised away.

=== current code
volatile struct bus_master_interface *interface;
while (interface->ack != OK) {
ÂÂÂÂÂÂÂÂdelay(a short while) ÂÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂ[ after X loops device changes interface->ack by dma ]
};

=== after conversion
struct bus_master_interface *interface;
while (interface->ack != OK) {
ÂÂÂÂÂÂÂÂdelay(a short while);
ÂÂÂÂÂÂÂÂrmb();
[ after X loops device changes interface->ack by dma ]
};

All I need is for the read of interface->ack in the loop not to be optimised
away - is rmb() the appropriate incantation to achieve this?

2) Writing to memory, interrupt device
Need command to be in memory for device to read by DMA before device gets
interrupted.

=== current code ===
volatile struct bus_master_interface *interface;
interface->cmd = command;
iowrite(device_interrupt, 1);
[device reads interface->cmd by dma]

=== after conversion ===
struct bus_master_interface *interface;
interface->cmd = command;
wmb();
iowrite(device_interrupt, 1);
[device reads interface->cmd by dma]

Is the wmb() a guarantee that the command will be in memory visible to the
device when the driver informs it of a new command?
Is it even needed? I.e. does iowrite() effectively form a barrier?

regards

--
Eliot Blennerhassett
www.audioscience.com
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/