Skip to contentSkip to conversation

Best practice to lock a read/write to a HW register

DISCUSSION Luca Ellero 5 participants 6 messages Nov 14, 2013 – Nov 15, 2013

lore
Share & tools
lore
#0 lore raw

Hi all,
can someone please show me which is the best practice to lock a read/write to a hardware register.
In other words if, in a driver, I want to modify a bit in a HW register, I have to read the register, set/reset the relevant bit and write back the reg.
But what can I do to be sure that no other code modifies the register between my read and write?
Is spin_lock() suitable for this purpose?
Thanks
Best regards
Luca

#1 lore raw
Quoted 9 lines · Luca Ellero

Hi all,
can someone please show me which is the best practice to lock a read/write to a hardware register.
In other words if, in a driver, I want to modify a bit in a HW register, I have to read the register, set/reset the relevant bit and write back the reg.
But what can I do to be sure that no other code modifies the register between my read and write?
Is spin_lock() suitable for this purpose?

If IRQ handlers can touch this register, then I think you'll have to disable interrupts as well (spin_lock_irq*() ?).

HTH,
-mandep

Quoted 9 lines

Thanks
Best regards
Luca

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

#2 lore raw
In reply to Luca Ellero

Hi Luca

The locking mechanism to be used depends on what your piece of code does and where your code runs.
If your code runs in a interrupt context (inside an interrupt handler), you should use a spinlock (with irq save and restore). Else you can use mutex or a semaphore.

In either cases, you don't lock the hardware register but create a lock descriptor and lock the same. So you need to implement locking at every place the register is accessed.

Please refer http://lwn.net/Kernel/LDD3/ on how to use locks.

Regards,
Binoy Jayan

________________________________________
From: [email protected] [kernelnewbies-bounces at kernelnewbies.org] on behalf of Luca Ellero [lroluk at gmail.com]
Sent: Thursday, November 14, 2013 4:02 PM
To: kernelnewbies at kernelnewbies.org
Subject: Best practice to lock a read/write to a HW register

Hi all,
can someone please show me which is the best practice to lock a read/write to a hardware register.
In other words if, in a driver, I want to modify a bit in a HW register, I have to read the register, set/reset the relevant bit and write back the reg.
But what can I do to be sure that no other code modifies the register between my read and write?
Is spin_lock() suitable for this purpose?
Thanks
Best regards
Luca

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
Please do not print this email unless it is absolutely necessary.

The information contained in this electronic message and any attachments to this message are intended for the exclusive use of the addressee(s) and may contain proprietary, confidential or privileged information. If you are not the intended recipient, you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately and destroy all copies of this message and any attachments.

WARNING: Computer viruses can be transmitted via email. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email.

www.wipro.com

#3 lore raw
In reply to Luca Ellero

Hi!

On Don, 2013-11-14 at 11:32 +0100, Luca Ellero wrote:
[...]

Quoted 8 lines

can someone please show me which is the best practice to lock a read/write to a hardware register.
In other words if, in a driver, I want to modify a bit in a HW register, I have to read the register, set/reset the relevant bit and write back the reg.
But what can I do to be sure that no other code modifies the register between my read and write?
Is spin_lock() suitable for this purpose?

If the read-modify-write (read: the critical section) run-time is short, a spin-lock is the usual means. If it is needed from IRQ-context - see also other mails - you must use the *_irq() variant.

Bernd

Signature · 2 lines
Bernd Petrovitsch                  Email : bernd at petrovitsch.priv.at
                     LUGA : http://www.luga.at
#4 lore raw

Ideally, it depends on the platform you are working. You need to discuss with them for the correct approach. For Qualcomm they have some hardware caled as RPU(Register protection unit). They have systems calls exposed for using them. You can associtate it to the register you want protection. I dont know if there is any way you could do it in Linux itself?

Regards,
Sandeep

Quoted 23 lines · Bernd Petrovitsch

Hi!

On Don, 2013-11-14 at 11:32 +0100, Luca Ellero wrote:
[...]

Quoted 8 lines

can someone please show me which is the best practice to lock a read/write to a hardware register.
In other words if, in a driver, I want to modify a bit in a HW register, I have to read the register, set/reset the relevant bit and write back the reg.
But what can I do to be sure that no other code modifies the register between my read and write?
Is spin_lock() suitable for this purpose?

If the read-modify-write (read: the critical section) run-time is short, a spin-lock is the usual means. If it is needed from IRQ-context - see also other mails - you must use the *_irq() variant.

Bernd
--
Bernd Petrovitsch Email : bernd at petrovitsch.priv.at
LUGA : http://www.luga.at

Quoted 5 lines

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20131115/3e88aab8/attachment-0001.html

#5 lore raw
Quoted 42 lines · manty kuma ha

Ideally, it depends on the platform you are working. You need to discuss with them for the correct approach. For Qualcomm they have some hardware caled as RPU(Register protection unit). They have systems calls exposed for using them. You can associtate it to the register you want protection. I dont know if there is any way you could do it in Linux itself?

Regards,
Sandeep

On Thu, Nov 14, 2013 at 9:14 PM, Bernd Petrovitsch
<bernd at petrovitsch.priv.at <mailto:[email protected]>> wrote:

Hi!

On Don, 2013-11-14 at 11:32 +0100, Luca Ellero wrote:
[...]
> can someone please show me which is the best practice to lock a > read/write to a hardware register.
> In other words if, in a driver, I want to modify a bit in a HW register,
> I have to read the register, set/reset the relevant bit and write back
> the reg.
> But what can I do to be sure that no other code modifies the register
> between my read and write?
> Is spin_lock() suitable for this purpose?

If the read-modify-write (read: the critical section) run-time is short,
a spin-lock is the usual means. If it is needed from IRQ-context - see also other mails - you must use the *_irq() variant.

Bernd
--
Bernd Petrovitsch Email :
bernd at petrovitsch.priv.at <mailto:[email protected]>
LUGA : http://www.luga.at

Quoted 7 lines

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
<mailto:[email protected]>
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

Thanks to all,
your suggestions are very appreciated
Best regards
Luca