DataMemoryBarrier

Discussion and questions about programming with Ultibo.
Koen
Posts: 31
Joined: Sat Apr 29, 2017 10:41 am

DataMemoryBarrier

Postby Koen » Tue Aug 21, 2018 11:02 am

I 've been reading about the DataMemoryBarrier instruction on ARM but i 'm not sure about when to use it.
If i use my own GPIO pin acces (in Ultibo) writing directly to the GPIO registers do i have to insert a DataMemoryBarrier instruction?
I see you do so in e.g. unit BCM2709.
As far as i understand it would do no harm if i use it, but is it necessary?
User avatar
Ultibo
Site Admin
Posts: 2040
Joined: Sat Dec 19, 2015 3:49 am
Location: Australia

Re: DataMemoryBarrier

Postby Ultibo » Tue Aug 21, 2018 11:48 am

Koen wrote:I 've been reading about the DataMemoryBarrier instruction on ARM but i 'm not sure about when to use it.
If i use my own GPIO pin acces (in Ultibo) writing directly to the GPIO registers do i have to insert a DataMemoryBarrier instruction?
I see you do so in e.g. unit BCM2709.

Hi,

There are two parts to that questions, ARM themselves have certain documentation that says when a DataMemoryBarrier (DMB) should be used and Broadcom (who produced the Pi SoC) also have documentation that says when a DMB should be used.

According to page 7 of the Broadcom documentation a data memory barrier should be used before the first write to a peripheral and after the last read of a peripheral.

We add those as per the documentation in our GPIO driver (and all of the memory mapped drivers like I2C, SPI, PWM etc), but Ultibo is a preemptive multitasking environment and the interrupt handler also places a data memory barrier before and after interrupt functions which means that in many cases they could be left out of the driver code completely and you would probably not notice any difference.

Koen wrote:As far as i understand it would do no harm if i use it, but is it necessary?

That is a very difficult question to answer, the point of the memory barrier is to ensure that certain operations are completed before other operations are allowed to occur.

While developing the very low level core functions of Ultibo we actually ran into a case with the spin lock and mutex code that exactly showed the case that ARM warns about in their documentation and (after several days debugging) we were able to prove the cause of a particular bug down to the need for a DMB before a specific operation.

In most other cases it is much more difficult to see or prove, what we do know is that the reason for the DMB is real and both ARM and Broadcom (who know a lot more about the CPU than we do) give specific guidance on when it should be used. The other thing we know for sure is that many other people doing bare metal development run into a lot of problems that we don't see so for now we are going to stick with doing what we are doing.
Ultibo.org | Make something amazing
https://ultibo.org
Koen
Posts: 31
Joined: Sat Apr 29, 2017 10:41 am

Re: DataMemoryBarrier

Postby Koen » Tue Aug 21, 2018 4:09 pm

Thanks for replying.
I 'll add the DataMemoryBarrier to my code.

Return to “General”

Who is online

Users browsing this forum: No registered users and 0 guests