kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
* Best practice to lock a read/write to a HW register
@ 2013-11-14 10:32 Luca Ellero
  2013-11-14 11:20 ` Mandeep Sandhu
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Luca Ellero @ 2013-11-14 10:32 UTC (permalink / raw)
  To: kernelnewbies

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

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

* Best practice to lock a read/write to a HW register
  2013-11-14 10:32 Best practice to lock a read/write to a HW register Luca Ellero
@ 2013-11-14 11:20 ` Mandeep Sandhu
  2013-11-14 11:51 ` binoy.jayan at wipro.com
  2013-11-14 12:14 ` Bernd Petrovitsch
  2 siblings, 0 replies; 6+ messages in thread
From: Mandeep Sandhu @ 2013-11-14 11:20 UTC (permalink / raw)
  To: kernelnewbies

On Thu, Nov 14, 2013 at 4:02 PM, Luca Ellero <lroluk@gmail.com> wrote:
> 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

> Thanks
> Best regards
> Luca
>
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

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

* Best practice to lock a read/write to a HW register
  2013-11-14 10:32 Best practice to lock a read/write to a HW register Luca Ellero
  2013-11-14 11:20 ` Mandeep Sandhu
@ 2013-11-14 11:51 ` binoy.jayan at wipro.com
  2013-11-14 12:14 ` Bernd Petrovitsch
  2 siblings, 0 replies; 6+ messages in thread
From: binoy.jayan at wipro.com @ 2013-11-14 11:51 UTC (permalink / raw)
  To: kernelnewbies


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: kernelnewbies-bounces@kernelnewbies.org [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

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

* Best practice to lock a read/write to a HW register
  2013-11-14 10:32 Best practice to lock a read/write to a HW register Luca Ellero
  2013-11-14 11:20 ` Mandeep Sandhu
  2013-11-14 11:51 ` binoy.jayan at wipro.com
@ 2013-11-14 12:14 ` Bernd Petrovitsch
  2013-11-15  1:30   ` manty kuma
  2 siblings, 1 reply; 6+ messages in thread
From: Bernd Petrovitsch @ 2013-11-14 12:14 UTC (permalink / raw)
  To: kernelnewbies

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
                     LUGA : http://www.luga.at

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

* Best practice to lock a read/write to a HW register
  2013-11-14 12:14 ` Bernd Petrovitsch
@ 2013-11-15  1:30   ` manty kuma
  2013-11-15  8:53     ` Luca Ellero
  0 siblings, 1 reply; 6+ messages in thread
From: manty kuma @ 2013-11-15  1:30 UTC (permalink / raw)
  To: kernelnewbies

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@petrovitsch.priv.at> 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
>                      LUGA : http://www.luga.at
>
>
> _______________________________________________
> 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 

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

* Best practice to lock a read/write to a HW register
  2013-11-15  1:30   ` manty kuma
@ 2013-11-15  8:53     ` Luca Ellero
  0 siblings, 0 replies; 6+ messages in thread
From: Luca Ellero @ 2013-11-15  8:53 UTC (permalink / raw)
  To: kernelnewbies

Il 15/11/2013 02:30, manty kuma ha wrote:
> 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:bernd@petrovitsch.priv.at>> 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:bernd@petrovitsch.priv.at>
>                          LUGA : http://www.luga.at
>
>
>     _______________________________________________
>     Kernelnewbies mailing list
>     Kernelnewbies at kernelnewbies.org
>     <mailto:Kernelnewbies@kernelnewbies.org>
>     http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
>


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

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

end of thread, other threads:[~2013-11-15  8:53 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-11-14 10:32 Best practice to lock a read/write to a HW register Luca Ellero
2013-11-14 11:20 ` Mandeep Sandhu
2013-11-14 11:51 ` binoy.jayan at wipro.com
2013-11-14 12:14 ` Bernd Petrovitsch
2013-11-15  1:30   ` manty kuma
2013-11-15  8:53     ` Luca Ellero

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).