public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Q: volatile vs barriers to access memory data changed by device DMA
@ 2008-02-25  1:04 Eliot Blennerhassett
  2008-02-25  8:32 ` Alan Cox
  0 siblings, 1 reply; 2+ messages in thread
From: Eliot Blennerhassett @ 2008-02-25  1:04 UTC (permalink / raw)
  To: linux-kernel

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Q: volatile vs barriers to access memory data changed by device DMA
  2008-02-25  1:04 Q: volatile vs barriers to access memory data changed by device DMA Eliot Blennerhassett
@ 2008-02-25  8:32 ` Alan Cox
  0 siblings, 0 replies; 2+ messages in thread
From: Alan Cox @ 2008-02-25  8:32 UTC (permalink / raw)
  To: Eliot Blennerhassett; +Cc: linux-kernel

> === 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?

Yes - ish.  You want the equivalent of 

	do {
		rmb();
		if (interface->ack == OK)
			break;
	} while(1);

(eg putting another rmb before the while in your case)

You want a barrier before the *first* read in case the
compiler has managed to cache the value before you enter the loop.


> struct bus_master_interface *interface;
> interface->cmd = command;
> wmb();
> iowrite(device_interrupt, 1);

Yes.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2008-02-25  8:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-02-25  1:04 Q: volatile vs barriers to access memory data changed by device DMA Eliot Blennerhassett
2008-02-25  8:32 ` Alan Cox

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox