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

Alan Cox
2008-02-25T08:42:18Z

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