public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Eliot Blennerhassett <linux@audioscience.com>
To: linux-kernel@vger.kernel.org
Subject: Q: volatile vs barriers to access memory data changed by device DMA
Date: Mon, 25 Feb 2008 14:04:01 +1300	[thread overview]
Message-ID: <200802251404.01874.linux@audioscience.com> (raw)

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

             reply	other threads:[~2008-02-25  1:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-25  1:04 Eliot Blennerhassett [this message]
2008-02-25  8:32 ` Q: volatile vs barriers to access memory data changed by device DMA Alan Cox

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200802251404.01874.linux@audioscience.com \
    --to=linux@audioscience.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox