DSPRelated.com
Forums
Online · Live + on demand
February 9-11, 2027
Price goes up Jan 1: register now to lock in today's rate
Register Now →

Atomic operations on C6x family of processors

Started by "Manuel E. Cotallo Torres" ●January 19, 2007
Hi Tom, Adolf,

Thank you for pointing me out to that! Are you referring to the
bottom of page 8-11 and top of page 8-12 of SPRU189F? Is it the
EP(n+3) and EP(n+4) in figures 8-12 and 8-13 of the document, that
may clear GIE without affecting the processing of the interrupt;
which results in PGIE=0 in clock cycle 6?

If my guess is correct then now I understtod the purpose of the PGIE bit.
This is quite a miserable (what other adjective to use?) feature,
which looks more like a bug...

How strange is the last condition on page 8-19 regarding a branch
in EP(n+4), as this EP is in the DC phase on clock cycle 4; and how
does the CPU know that it may contain B instruction before decoding? :)

A solution to this problem is more or less obvious: it is to always
clear/set GIE in a subroutine right during the delay slots of its
return branch. E.g.:

int Disable_Interrupts (void); // returns previous CSR

.TEXT
.GLOBAL _Disable_Interrupts

_Disable_Interrupts: ; asm code
B B3 ; begin return
MVC CSR, B0 ; read CSR
MV B0, A4 ; store previous CSR into return register
|| AND -2, B0, B0 ; clear GIE bit
MVC B0, CSR ; modify CSR
NOP ; branch delay slot 4
NOP ; branch delay slot 5

.END

I take back my words that doing something in a branch delay slots must
be avoided. It has to be used and sometimes it might be the only way to
do something right!

Thanks, take care,

Andrew

> Date: Mon, 22 Jan 2007 10:06:54 -0800 (PST)
> From: Tom Kerekes Adolf,
>
> I totally agree. That reminds me of a problem I had with disabling
> interrupts on the C6x that drove me CRAZY. On one system I use an interrupt
> and my own OS to switch tasks (threads). Common sense would indicate that if
> the interrupt occurred that interrupts had to be enabled - not true. Because
> of the latency you describe, the interrupt can occur after interrupts are
> disabled. In this case, when returning from the interrupt, interrupts need
> to be disabled instead of enabled. The C6x handles this with the PGIE bit.
> My problem occurred when I was interrupted from code that had just turned off
> interrupts and was expecting to quickly turn back on interrupts, but then
> switched to another thread that expected interrupts to just be on. The
> result was that interrupts were turned off permanently! After that I
> decided to leave GIE alone whenever possible...
>
> Tom Kerekes
> DynoMotion, Inc.
> ----- Original Message ----
> From: Adolf Klemenz
> Sent: Monday, January 22, 2007 6:32:40 AM
>
> Dear Andrew,
>
> of course you're right - using the branch delay slots is effectively
> identical to disabling interrupts and is by no means a real atomic
> operation. However, safely disabling interrupts by clearing the global
> interrupt enable (GIE) bit in the C6000 status register requires some
> precautions: If an interrupt occurs simultaneously with the mvc instruction
> which writes the status register, it is still executed! Additional nops are
> required between clearing the GIE and the read/modify/ write of a memory
> location.
> On the other hand, interrupts are automatically disabled once a branch
> instruction is in the pipeline. This is a feature of the C6000 core
> hardware which can be used to emulate atomic operations, although it surely
> has never been intended for this purpose.
> I don't know how x86 or similar CPUs implement atomic operations, but it is
> quite likely that these will suspend interrupts in hardware too. From a
> code portability point of view, I admit, using the branch delay slots is
> bad practice!
>
> Best Regards,
> Adolf Klemenz, D.SignT
>
> At 03:02 22.01.2007 -0800, Andrew Nesterov wrote:
>
>Dear All,
>
>An interesting discussion. Let me add my $0.02 :)
>
>I snipped out large portions of the reply posts just to get to
>the essence of the problem.
>
>The original question posted was:
>
> > Posted by: "Manuel E. Cotallo Torres"
> mcotallo@sicubo. com
> >
> > how to perform atomic compare-and- swap operations, like in
> > Intel x86 family cmpxchg operations, locking the access to memory for
> > threaded concurrent operations but without the impact of
> > disabling/enabling interrupts?
>
>These are the proposed solutions:
>
> > Posted by: "Adolf Klemenz"
> adolf.klemenz@ dsignt.de
> >
> > the only solution I know is to pack all the code into the five so called
> > delay slots of a branch instruction. These delay slots are not
> > interruptable.
>
> > Posted by: "Tom Kerekes" tksoft@yahoo. com TKSOFT
> >
> > Since the C6x will not interrupt while a branch is in progress, I often
> take
> > advantage of that for these cases.
>
> > Posted by: "Jeff Brower"
> jbrower@signalogic. com jbrower888
> >
> > Adolf's solution is very creative. If you get this to work, please let
> us know.
>
>Yep, it is a clever way, and no doubt it does work, but it does not solve
>the original question not in a micron :)
>
>The whole point is that C6000 does not have an atomic compare-n-swap
>operation. Generally, it is not doable to obtain an exclusive access to
>memory in a multitasking mode for the C6000 architecture.
>
>Any solution is a variant of the exact algorithm: 1) disable interrupts
>2) do whatever is needed in the single process mode and 3) restore interrupts.
>
>It really does not matter how interrupts were disabled, the point is
>that they were disabled.
>
>So I wouldn't try do it in the complicated way of nesting several branches,
>which is from my point of view an error-prone programming, instead I would
>use the plain way of changing GIE. OTH, it is completely valid method to
>do something during branch delay slots, no doubts.
>
>I feel uncomfortable that I used so many imperative statements in my email,
>but only for the purpose of clarifying this interesting problem. Please
>accept my apologies.
>
>Rgds,
>
>Andrew
Hi Andrew,

spru189f (CPU and instruction reference) states on page 8-25: "When an
interrupt occurs, all instructions entering E1 prior to the beginning of
interrupt processing are allowed to complete execution (through E5)"

Hence, some instructions following a mvc (which clears the global interrupt
enable bit) may still be interrupted. Your solution securely avoids this
problem - another option is adding sufficient "nops" after the mvc instruction.

Interrupt processing on the C6000 processors has quite some pitfalls,
especially if software pipelining is used extensively. This code will often
rely on registers holding different data in different pipeline stages. Such
code cannot be interrupted without disrupting the (software) pipeline. For
this reason almost all dsplib amd imglib functions disable interrupts.

The C6000 DSPs use a simple and straightforward core design which allows
high speed and low power consumption, but it leaves handling of all those
pipeline issues to the compiler - or the assembler programmer :((

Best Regards,
Adolf Klemenz, D.SignT
At 05:47 24.01.2007 -0800, Andrew Nesterov wrote:

>Hi Tom, Adolf,
>
>Thank you for pointing me out to that! Are you referring to the
>bottom of page 8-11 and top of page 8-12 of SPRU189F? Is it the
>EP(n+3) and EP(n+4) in figures 8-12 and 8-13 of the document, that
>may clear GIE without affecting the processing of the interrupt;
>which results in PGIE=0 in clock cycle 6?
>
>If my guess is correct then now I understtod the purpose of the PGIE bit.
>This is quite a miserable (what other adjective to use?) feature,
>which looks more like a bug...
>
>How strange is the last condition on page 8-19 regarding a branch
>in EP(n+4), as this EP is in the DC phase on clock cycle 4; and how
>does the CPU know that it may contain B instruction before decoding? :)
>
>A solution to this problem is more or less obvious: it is to always
>clear/set GIE in a subroutine right during the delay slots of its
>return branch. E.g.:
>
>int Disable_Interrupts (void); // returns previous CSR
>
> .TEXT
> .GLOBAL _Disable_Interrupts
>
>_Disable_Interrupts: ; asm code
> B B3 ; begin return
> MVC CSR, B0 ; read CSR
> MV B0, A4 ; store previous CSR into return register
>|| AND -2, B0, B0 ; clear GIE bit
> MVC B0, CSR ; modify CSR
> NOP ; branch delay slot 4
> NOP ; branch delay slot 5
>
> .END
>
>I take back my words that doing something in a branch delay slots must
>be avoided. It has to be used and sometimes it might be the only way to
>do something right!
>
>Thanks, take care,
>
>Andrew
>
>>Date: Mon, 22 Jan 2007 10:06:54 -0800 (PST)
>>From: Tom Kerekes
>>Adolf,
>>I totally agree. That reminds me of a problem I had with disabling
>>interrupts on the C6x that drove me CRAZY. On one system I use an
>>interrupt and my own OS to switch tasks (threads). Common sense would
>>indicate that if the interrupt occurred that interrupts had to be enabled
>>- not true. Because of the latency you describe, the interrupt can occur
>>after interrupts are disabled. In this case, when returning from the
>>interrupt, interrupts need to be disabled instead of enabled. The C6x
>>handles this with the PGIE bit. My problem occurred when I was
>>interrupted from code that had just turned off interrupts and was
>>expecting to quickly turn back on interrupts, but then switched to
>>another thread that expected interrupts to just be on. The result was
>>that interrupts were turned off permanently! After that I decided to
>>leave GIE alone whenever possible...
>>Tom Kerekes
>>DynoMotion, Inc.
>>----- Original Message ----
>>From: Adolf Klemenz
>>Sent: Monday, January 22, 2007 6:32:40 AM
>>
>>Dear Andrew,
>>
>>of course you're right - using the branch delay slots is effectively
>>identical to disabling interrupts and is by no means a real atomic
>>operation. However, safely disabling interrupts by clearing the global
>>interrupt enable (GIE) bit in the C6000 status register requires some
>>precautions: If an interrupt occurs simultaneously with the mvc
>>instruction which writes the status register, it is still executed!
>>Additional nops are required between clearing the GIE and the
>>read/modify/ write of a memory location.
>>On the other hand, interrupts are automatically disabled once a branch
>>instruction is in the pipeline. This is a feature of the C6000 core
>>hardware which can be used to emulate atomic operations, although it
>>surely has never been intended for this purpose.
>>I don't know how x86 or similar CPUs implement atomic operations, but it
>>is quite likely that these will suspend interrupts in hardware too. From
>>a code portability point of view, I admit, using the branch delay slots
>>is bad practice!
>>
>>Best Regards,
>>Adolf Klemenz, D.SignT
>>
>>At 03:02 22.01.2007 -0800, Andrew Nesterov wrote:
>>
>>Dear All,
>>
>>An interesting discussion. Let me add my $0.02 :)
>>
>>I snipped out large portions of the reply posts just to get to
>>the essence of the problem.
>>
>>The original question posted was:
>>
>> > Posted by: "Manuel E. Cotallo Torres" >> com>mcotallo@sicubo. com
>> >
>> > how to perform atomic compare-and- swap operations, like in
>> > Intel x86 family cmpxchg operations, locking the access to memory for
>> > threaded concurrent operations but without the impact of
>> > disabling/enabling interrupts?
>>
>>These are the proposed solutions:
>>
>> > Posted by: "Adolf Klemenz" >> .de>adolf.klemenz@ dsignt.de
>> >
>> > the only solution I know is to pack all the code into the five so called
>> > delay slots of a branch instruction. These delay slots are not
>> > interruptable.
>>
>> > Posted by: "Tom Kerekes" tksoft@yahoo. com
>> TKSOFT
>> >
>> > Since the C6x will not interrupt while a branch is in progress, I
>> often take
>> > advantage of that for these cases.
>>
>> > Posted by: "Jeff Brower" >> com>jbrower@signalogic. com jbrower888
>> >
>> > Adolf's solution is very creative. If you get this to work, please let
>> us know.
>>
>>Yep, it is a clever way, and no doubt it does work, but it does not solve
>>the original question not in a micron :)
>>
>>The whole point is that C6000 does not have an atomic compare-n-swap
>>operation. Generally, it is not doable to obtain an exclusive access to
>>memory in a multitasking mode for the C6000 architecture.
>>
>>Any solution is a variant of the exact algorithm: 1) disable interrupts
>>2) do whatever is needed in the single process mode and 3) restore
>>interrupts.
>>
>>It really does not matter how interrupts were disabled, the point is
>>that they were disabled.
>>
>>So I wouldn't try do it in the complicated way of nesting several branches,
>>which is from my point of view an error-prone programming, instead I would
>>use the plain way of changing GIE. OTH, it is completely valid method to
>>do something during branch delay slots, no doubts.
>>
>>I feel uncomfortable that I used so many imperative statements in my email,
>>but only for the purpose of clarifying this interesting problem. Please
>>accept my apologies.
>>
>>Rgds,
>>
>>Andrew
>
I just noticed that by mistake I've sent two variants of the same post.
Sorry for that. The most resent one is the right one.

Rgds,

Andrew
Adolf/Andrew,

Disable interrupts to disable interrupts !! Ha Ha...

But I'm not 100% convinced yet. I'm never able to follow those pipeline diagrams like in those Figures. I'm wondering if there is still a small window after the

> MVC B0, CSR ; modify CSR

instruction to get interrupted before the CSR is actually modified. The branch will only hold off interrupts for two more cycles. I'm thinking it might be 3~5 cycles later before the CSR is actually modified. Anybody know the answer to this?

Tom Kerekes
DynoMotion, Inc.
----- Original Message ----
From: Adolf Klemenz
To: Andrew Nesterov ; Tom Kerekes
Cc: c...
Sent: Wednesday, January 24, 2007 8:10:36 AM
Subject: Re: Atomic operations on C6x family of processors
Hi Andrew,

spru189f (CPU and instruction reference) states on page 8-25: "When an
interrupt occurs, all instructions entering E1 prior to the beginning of
interrupt processing are allowed to complete execution (through E5)"

Hence, some instructions following a mvc (which clears the global interrupt
enable bit) may still be interrupted. Your solution securely avoids this
problem - another option is adding sufficient "nops" after the mvc instruction.

Interrupt processing on the C6000 processors has quite some pitfalls,
especially if software pipelining is used extensively. This code will often
rely on registers holding different data in different pipeline stages. Such
code cannot be interrupted without disrupting the (software) pipeline. For
this reason almost all dsplib amd imglib functions disable interrupts.

The C6000 DSPs use a simple and straightforward core design which allows
high speed and low power consumption, but it leaves handling of all those
pipeline issues to the compiler - or the assembler programmer :((

Best Regards,
Adolf Klemenz, D.SignT
At 05:47 24.01.2007 -0800, Andrew Nesterov wrote:

>Hi Tom, Adolf,
>
>Thank you for pointing me out to that! Are you referring to the
>bottom of page 8-11 and top of page 8-12 of SPRU189F? Is it the
>EP(n+3) and EP(n+4) in figures 8-12 and 8-13 of the document, that
>may clear GIE without affecting the processing of the interrupt;
>which results in PGIE=0 in clock cycle 6?
>
>If my guess is correct then now I understtod the purpose of the PGIE bit.
>This is quite a miserable (what other adjective to use?) feature,
>which looks more like a bug...
>
>How strange is the last condition on page 8-19 regarding a branch
>in EP(n+4), as this EP is in the DC phase on clock cycle 4; and how
>does the CPU know that it may contain B instruction before decoding? :)
>
>A solution to this problem is more or less obvious: it is to always
>clear/set GIE in a subroutine right during the delay slots of its
>return branch. E.g.:
>
>int Disable_Interrupts (void); // returns previous CSR
>
> .TEXT
> .GLOBAL _Disable_Interrupts
>
>_Disable_Interrupts: ; asm code
> B B3 ; begin return
> MVC CSR, B0 ; read CSR
> MV B0, A4 ; store previous CSR into return register
>|| AND -2, B0, B0 ; clear GIE bit
> MVC B0, CSR ; modify CSR
> NOP ; branch delay slot 4
> NOP ; branch delay slot 5
>
> .END
>
>I take back my words that doing something in a branch delay slots must
>be avoided. It has to be used and sometimes it might be the only way to
>do something right!
>
>Thanks, take care,
>
>Andrew
>
>>Date: Mon, 22 Jan 2007 10:06:54 -0800 (PST)
>>From: Tom Kerekes
>>Adolf,
>>I totally agree. That reminds me of a problem I had with disabling
>>interrupts on the C6x that drove me CRAZY. On one system I use an
>>interrupt and my own OS to switch tasks (threads). Common sense would
>>indicate that if the interrupt occurred that interrupts had to be enabled
>>- not true. Because of the latency you describe, the interrupt can occur
>>after interrupts are disabled. In this case, when returning from the
>>interrupt, interrupts need to be disabled instead of enabled. The C6x
>>handles this with the PGIE bit. My problem occurred when I was
>>interrupted from code that had just turned off interrupts and was
>>expecting to quickly turn back on interrupts, but then switched to
>>another thread that expected interrupts to just be on. The result was
>>that interrupts were turned off permanently! After that I decided to
>>leave GIE alone whenever possible...
>>Tom Kerekes
>>DynoMotion, Inc.
>>----- Original Message ----
>>From: Adolf Klemenz
>>Sent: Monday, January 22, 2007 6:32:40 AM
>>
>>Dear Andrew,
>>
>>of course you're right - using the branch delay slots is effectively
>>identical to disabling interrupts and is by no means a real atomic
>>operation. However, safely disabling interrupts by clearing the global
>>interrupt enable (GIE) bit in the C6000 status register requires some
>>precautions: If an interrupt occurs simultaneously with the mvc
>>instruction which writes the status register, it is still executed!
>>Additional nops are required between clearing the GIE and the
>>read/modify/ write of a memory location.
>>On the other hand, interrupts are automatically disabled once a branch
>>instruction is in the pipeline. This is a feature of the C6000 core
>>hardware which can be used to emulate atomic operations, although it
>>surely has never been intended for this purpose.
>>I don't know how x86 or similar CPUs implement atomic operations, but it
>>is quite likely that these will suspend interrupts in hardware too. From
>>a code portability point of view, I admit, using the branch delay slots
>>is bad practice!
>>
>>Best Regards,
>>Adolf Klemenz, D.SignT
>>
>>At 03:02 22.01.2007 -0800, Andrew Nesterov wrote:
>>
>>Dear All,
>>
>>An interesting discussion. Let me add my $0.02 :)
>>
>>I snipped out large portions of the reply posts just to get to
>>the essence of the problem.
>>
>>The original question posted was:
>>
>> > Posted by: "Manuel E. Cotallo Torres" >> com>mcotallo@sicubo. com
>> >
>> > how to perform atomic compare-and- swap operations, like in
>> > Intel x86 family cmpxchg operations, locking the access to memory for
>> > threaded concurrent operations but without the impact of
>> > disabling/enabling interrupts?
>>
>>These are the proposed solutions:
>>
>> > Posted by: "Adolf Klemenz" >> .de>adolf.klemenz@ dsignt.de
>> >
>> > the only solution I know is to pack all the code into the five so called
>> > delay slots of a branch instruction. These delay slots are not
>> > interruptable.
>>
>> > Posted by: "Tom Kerekes" tksoft@yahoo. com
>> TKSOFT
>> >
>> > Since the C6x will not interrupt while a branch is in progress, I
>> often take
>> > advantage of that for these cases.
>>
>> > Posted by: "Jeff Brower" >> com>jbrower@signalogic. com jbrower888
>> >
>> > Adolf's solution is very creative. If you get this to work, please let
>> us know.
>>
>>Yep, it is a clever way, and no doubt it does work, but it does not solve
>>the original question not in a micron :)
>>
>>The whole point is that C6000 does not have an atomic compare-n-swap
>>operation. Generally, it is not doable to obtain an exclusive access to
>>memory in a multitasking mode for the C6000 architecture.
>>
>>Any solution is a variant of the exact algorithm: 1) disable interrupts
>>2) do whatever is needed in the single process mode and 3) restore
>>interrupts.
>>
>>It really does not matter how interrupts were disabled, the point is
>>that they were disabled.
>>
>>So I wouldn't try do it in the complicated way of nesting several branches,
>>which is from my point of view an error-prone programming, instead I would
>>use the plain way of changing GIE. OTH, it is completely valid method to
>>do something during branch delay slots, no doubts.
>>
>>I feel uncomfortable that I used so many imperative statements in my email,
>>but only for the purpose of clarifying this interesting problem. Please
>>accept my apologies.
>>
>>Rgds,
>>
>>Andrew
>
Hi Adolf,

> Date: Wed, 24 Jan 2007 17:10:36 +0100
> From: Adolf Klemenz Hi Andrew,
>
> spru189f (CPU and instruction reference) states on page 8-25: "When an
> interrupt occurs, all instructions entering E1 prior to the beginning of
> interrupt processing are allowed to complete execution (through E5)"

Yes, but strictly speaking this statement is in relation to single assignement
vs multiple assignement code. The problem that Tom described exists even
in the interruptible single assignement code.

The "feature" that blocking interrupts may take one or two additional clock
cycles after the MVC instruction may pose a problem with synchronization/shared
access in the single process with multiple ISRs model:

If both the process and an ISR test and modify the same variable (a mutex),
they both may erroneously obtain the same resource, due to non-atomic
method of the "test and modify" operation.

Yet another problem, as it has been detected and described by Tom, starts when
the application consists of many concurrent processes and a context switch
occurs.

As the result of this particular situation, when PGIE is zero right before
exiting an ISR (which in turn is the result of delays in interrupt disabling),
a process that has no knowledge that the interrupted process turned interrupts
off will take control and work in a system with disabled interrupts. This
is apparently a disaster.

In the absence of context switch this would never happen, since the interrupted
process does know that interrupts were previously disabled and would eventually
turn them on again.

> Hence, some instructions following a mvc (which clears the global interrupt
> enable bit) may still be interrupted. Your solution securely avoids this
> problem - another option is adding sufficient "nops" after the mvc
> instruction.

Adding NOP 2 after the MVC certainly solves the problem with test and modify
operation, but unfortunately does not solve the second problem of context switch
with disabled interrupts...

Rgds,

Andrew

> Interrupt processing on the C6000 processors has quite some pitfalls,
> especially if software pipelining is used extensively. This code will often
> rely on registers holding different data in different pipeline stages. Such
> code cannot be interrupted without disrupting the (software) pipeline. For
> this reason almost all dsplib amd imglib functions disable interrupts.

Fortunately there are many ways to get pipelined single assignement code,
which is interruptible. I am not particularly impressed with the approach
used in the dsplib and imglib to block interrupts during its computation,
as it may take a time long enough to miss a critical event.

> The C6000 DSPs use a simple and straightforward core design which allows high
> speed and low power consumption, but it leaves handling of all those pipeline
> issues to the compiler - or the assembler programmer :((

This makes it (programming) especially interesting :)

> Best Regards,
> Adolf Klemenz, D.SignT
> At 05:47 24.01.2007 -0800, Andrew Nesterov wrote:
>
>> Hi Tom, Adolf,
>>
>> Thank you for pointing me out to that! Are you referring to the
>> bottom of page 8-11 and top of page 8-12 of SPRU189F? Is it the
>> EP(n+3) and EP(n+4) in figures 8-12 and 8-13 of the document, that
>> may clear GIE without affecting the processing of the interrupt;
>> which results in PGIE=0 in clock cycle 6?
>>
>> If my guess is correct then now I understtod the purpose of the PGIE bit.
>> This is quite a miserable (what other adjective to use?) feature,
>> which looks more like a bug...
>>
>> How strange is the last condition on page 8-19 regarding a branch
>> in EP(n+4), as this EP is in the DC phase on clock cycle 4; and how
>> does the CPU know that it may contain B instruction before decoding? :)
>>
>> A solution to this problem is more or less obvious: it is to always
>> clear/set GIE in a subroutine right during the delay slots of its
>> return branch. E.g.:
>>
>> int Disable_Interrupts (void); // returns previous CSR
>>
>> .TEXT
>> .GLOBAL _Disable_Interrupts
>>
>> _Disable_Interrupts: ; asm code
>> B B3 ; begin return
>> MVC CSR, B0 ; read CSR
>> MV B0, A4 ; store previous CSR into return register
>> || AND -2, B0, B0 ; clear GIE bit
>> MVC B0, CSR ; modify CSR
>> NOP ; branch delay slot 4
>> NOP ; branch delay slot 5
>>
>> .END
>>
>> I take back my words that doing something in a branch delay slots must
>> be avoided. It has to be used and sometimes it might be the only way to
>> do something right!
>>
>> Thanks, take care,
>>
>> Andrew
>>
>>> Date: Mon, 22 Jan 2007 10:06:54 -0800 (PST)
>>> From: Tom Kerekes
>>> Adolf,
>>> I totally agree. That reminds me of a problem I had with disabling
>>> interrupts on the C6x that drove me CRAZY. On one system I use an
>>> interrupt and my own OS to switch tasks (threads). Common sense would
>>> indicate that if the interrupt occurred that interrupts had to be enabled
>>> - not true. Because of the latency you describe, the interrupt can occur
>>> after interrupts are disabled. In this case, when returning from the
>>> interrupt, interrupts need to be disabled instead of enabled. The C6x
>>> handles this with the PGIE bit. My problem occurred when I was interrupted
>>> from code that had just turned off interrupts and was expecting to quickly
>>> turn back on interrupts, but then switched to another thread that expected
>>> interrupts to just be on. The result was that interrupts were turned off
>>> permanently! After that I decided to leave GIE alone whenever possible...
>>> Tom Kerekes
>>> DynoMotion, Inc.
>>>
>>>
>>> ----- Original Message ----
>>> From: Adolf Klemenz
>>> Sent: Monday, January 22, 2007 6:32:40 AM
>>>
>>> Dear Andrew,
>>>
>>> of course you're right - using the branch delay slots is effectively
>>> identical to disabling interrupts and is by no means a real atomic
>>> operation. However, safely disabling interrupts by clearing the global
>>> interrupt enable (GIE) bit in the C6000 status register requires some
>>> precautions: If an interrupt occurs simultaneously with the mvc
>>> instruction which writes the status register, it is still executed!
>>> Additional nops are required between clearing the GIE and the read/modify/
>>> write of a memory location.
>>> On the other hand, interrupts are automatically disabled once a branch
>>> instruction is in the pipeline. This is a feature of the C6000 core
>>> hardware which can be used to emulate atomic operations, although it
>>> surely has never been intended for this purpose.
>>> I don't know how x86 or similar CPUs implement atomic operations, but it
>>> is quite likely that these will suspend interrupts in hardware too. From a
>>> code portability point of view, I admit, using the branch delay slots is
>>> bad practice!
>>>
>>> Best Regards,
>>> Adolf Klemenz, D.SignT
>>>
>>> At 03:02 22.01.2007 -0800, Andrew Nesterov wrote:
>>>
>>> Dear All,
>>>
>>> An interesting discussion. Let me add my $0.02 :)
>>>
>>> I snipped out large portions of the reply posts just to get to
>>> the essence of the problem.
>>>
>>> The original question posted was:
>>>
>>> > Posted by: "Manuel E. Cotallo Torres" >>> com>mcotallo@sicubo. com
>>> >
>>> > how to perform atomic compare-and- swap operations, like in
>>> > Intel x86 family cmpxchg operations, locking the access to memory for
>>> > threaded concurrent operations but without the impact of
>>> > disabling/enabling interrupts?
>>>
>>> These are the proposed solutions:
>>>
>>> > Posted by: "Adolf Klemenz" >>> .de>adolf.klemenz@ dsignt.de
>>> >
>>> > the only solution I know is to pack all the code into the five so called
>>> > delay slots of a branch instruction. These delay slots are not
>>> > interruptable.
>>>
>>> > Posted by: "Tom Kerekes" tksoft@yahoo. com
>>> TKSOFT
>>> >
>>> > Since the C6x will not interrupt while a branch is in progress, I often
>>> take
>>> > advantage of that for these cases.
>>>
>>> > Posted by: "Jeff Brower" >>> com>jbrower@signalogic. com jbrower888
>>> >
>>> > Adolf's solution is very creative. If you get this to work, please let
>>> us know.
>>>
>>> Yep, it is a clever way, and no doubt it does work, but it does not solve
>>> the original question not in a micron :)
>>>
>>> The whole point is that C6000 does not have an atomic compare-n-swap
>>> operation. Generally, it is not doable to obtain an exclusive access to
>>> memory in a multitasking mode for the C6000 architecture.
>>>
>>> Any solution is a variant of the exact algorithm: 1) disable interrupts
>>> 2) do whatever is needed in the single process mode and 3) restore
>>> interrupts.
>>>
>>> It really does not matter how interrupts were disabled, the point is
>>> that they were disabled.
>>>
>>> So I wouldn't try do it in the complicated way of nesting several
>>> branches,
>>> which is from my point of view an error-prone programming, instead I would
>>> use the plain way of changing GIE. OTH, it is completely valid method to
>>> do something during branch delay slots, no doubts.
>>>
>>> I feel uncomfortable that I used so many imperative statements in my
>>> email,
>>> but only for the purpose of clarifying this interesting problem. Please
>>> accept my apologies.
>>>
>>> Rgds,
>>>
>>> Andrew
>
Hi Tom,

> Date: Wed, 24 Jan 2007 15:45:00 -0800 (PST)
> From: Tom Kerekes Adolf/Andrew,
>
> Disable interrupts to disable interrupts !! Ha Ha...

This is close to a situation where the second error may kill the
first one, so the result is correct :)

> But I'm not 100% convinced yet. I'm never able to follow those pipeline
> diagrams like in those Figures. I'm wondering if there is still a small
> window after the
>
> MVC B0, CSR ; modify CSR
>
> instruction to get interrupted before the CSR is actually modified. The
> branch will only hold off interrupts for two more cycles. I'm thinking it
> might be 3~5 cycles later before the CSR is actually modified. Anybody know
> the answer to this?

The answer is yes, this should be safe. Moreover, the MVC can safely be placed
into the last (the fifth) delay slot of a branch. This directly follows from
the diagrams 8-12 and 8-13: if the condition 6 to take an interrupt is not
satisfied, then the interrupt is not taken. If the branch is in the EP(n-1)
on the diagram and MVC is in the EP(n+4), then on cycles 4,5 the condition
6 is false and on cycle 6 the condition 4 is false, hence the intrrupt is
not taken until GIE is set on again.

If the MVC is not in the delay slots of a branch, then the interrupt is
taken if the MVC is in EP(n+3) or EP(n+4) and in this case PGIE=0.

The same timing applies when the MVC clears a bit in the IER.

Rgds

Andrew

> Tom Kerekes
> DynoMotion, Inc.
----- Original Message ----
From: Adolf Klemenz
To: Andrew Nesterov ; Tom Kerekes
Cc: c...
Sent: Wednesday, January 24, 2007 8:10:36 AM
Subject: Re: Atomic operations on C6x family of processors
Hi Andrew,

spru189f (CPU and instruction reference) states on page 8-25: "When an
interrupt occurs, all instructions entering E1 prior to the beginning of
interrupt processing are allowed to complete execution (through E5)"

Hence, some instructions following a mvc (which clears the global interrupt
enable bit) may still be interrupted. Your solution securely avoids this
problem - another option is adding sufficient "nops" after the mvc instruction.

Interrupt processing on the C6000 processors has quite some pitfalls,
especially if software pipelining is used extensively. This code will often
rely on registers holding different data in different pipeline stages. Such
code cannot be interrupted without disrupting the (software) pipeline. For
this reason almost all dsplib amd imglib functions disable interrupts.

The C6000 DSPs use a simple and straightforward core design which allows
high speed and low power consumption, but it leaves handling of all those
pipeline issues to the compiler - or the assembler programmer :((

Best Regards,
Adolf Klemenz, D.SignT
At 05:47 24.01.2007 -0800, Andrew Nesterov wrote:

>Hi Tom, Adolf,
>
>Thank you for pointing me out to that! Are you referring to the
>bottom of page 8-11 and top of page 8-12 of SPRU189F? Is it the
>EP(n+3) and EP(n+4) in figures 8-12 and 8-13 of the document, that
>may clear GIE without affecting the processing of the interrupt;
>which results in PGIE=0 in clock cycle 6?
>
>If my guess is correct then now I understtod the purpose of the PGIE bit.
>This is quite a miserable (what other adjective to use?) feature,
>which looks more like a bug...
>
>How strange is the last condition on page 8-19 regarding a branch
>in EP(n+4), as this EP is in the DC phase on clock cycle 4; and how
>does the CPU know that it may contain B instruction before decoding? :)
>
>A solution to this problem is more or less obvious: it is to always
>clear/set GIE in a subroutine right during the delay slots of its
>return branch. E.g.:
>
>int Disable_Interrupts (void); // returns previous CSR
>
> .TEXT
> .GLOBAL _Disable_Interrupts
>
>_Disable_Interrupts: ; asm code
> B B3 ; begin return
> MVC CSR, B0 ; read CSR
> MV B0, A4 ; store previous CSR into return register
>|| AND -2, B0, B0 ; clear GIE bit
> MVC B0, CSR ; modify CSR
> NOP ; branch delay slot 4
> NOP ; branch delay slot 5
>
> .END
>
>I take back my words that doing something in a branch delay slots must
>be avoided. It has to be used and sometimes it might be the only way to
>do something right!
>
>Thanks, take care,
>
>Andrew
>
>>Date: Mon, 22 Jan 2007 10:06:54 -0800 (PST)
>>From: Tom Kerekes
>>Adolf,
>>I totally agree. That reminds me of a problem I had with disabling
>>interrupts on the C6x that drove me CRAZY. On one system I use an
>>interrupt and my own OS to switch tasks (threads). Common sense would
>>indicate that if the interrupt occurred that interrupts had to be enabled
>>- not true. Because of the latency you describe, the interrupt can occur
>>after interrupts are disabled. In this case, when returning from the
>>interrupt, interrupts need to be disabled instead of enabled. The C6x
>>handles this with the PGIE bit. My problem occurred when I was
>>interrupted from code that had just turned off interrupts and was
>>expecting to quickly turn back on interrupts, but then switched to
>>another thread that expected interrupts to just be on. The result was
>>that interrupts were turned off permanently! After that I decided to
>>leave GIE alone whenever possible...
>>Tom Kerekes
>>DynoMotion, Inc.
>>----- Original Message ----
>>From: Adolf Klemenz
>>Sent: Monday, January 22, 2007 6:32:40 AM
>>
>>Dear Andrew,
>>
>>of course you're right - using the branch delay slots is effectively
>>identical to disabling interrupts and is by no means a real atomic
>>operation. However, safely disabling interrupts by clearing the global
>>interrupt enable (GIE) bit in the C6000 status register requires some
>>precautions: If an interrupt occurs simultaneously with the mvc
>>instruction which writes the status register, it is still executed!
>>Additional nops are required between clearing the GIE and the
>>read/modify/ write of a memory location.
>>On the other hand, interrupts are automatically disabled once a branch
>>instruction is in the pipeline. This is a feature of the C6000 core
>>hardware which can be used to emulate atomic operations, although it
>>surely has never been intended for this purpose.
>>I don't know how x86 or similar CPUs implement atomic operations, but it
>>is quite likely that these will suspend interrupts in hardware too. From
>>a code portability point of view, I admit, using the branch delay slots
>>is bad practice!
>>
>>Best Regards,
>>Adolf Klemenz, D.SignT
>>
>>At 03:02 22.01.2007 -0800, Andrew Nesterov wrote:
>>
>>Dear All,
>>
>>An interesting discussion. Let me add my $0.02 :)
>>
>>I snipped out large portions of the reply posts just to get to
>>the essence of the problem.
>>
>>The original question posted was:
>>
>> > Posted by: "Manuel E. Cotallo Torres" >> com>mcotallo@sicubo. com
>> >
>> > how to perform atomic compare-and- swap operations, like in
>> > Intel x86 family cmpxchg operations, locking the access to memory for
>> > threaded concurrent operations but without the impact of
>> > disabling/enabling interrupts?
>>
>>These are the proposed solutions:
>>
>> > Posted by: "Adolf Klemenz" >> .de>adolf.klemenz@ dsignt.de
>> >
>> > the only solution I know is to pack all the code into the five so called
>> > delay slots of a branch instruction. These delay slots are not
>> > interruptable.
>>
>> > Posted by: "Tom Kerekes" tksoft@yahoo. com
>> TKSOFT
>> >
>> > Since the C6x will not interrupt while a branch is in progress, I
>> often take
>> > advantage of that for these cases.
>>
>> > Posted by: "Jeff Brower" >> com>jbrower@signalogic. com jbrower888
>> >
>> > Adolf's solution is very creative. If you get this to work, please let
>> us know.
>>
>>Yep, it is a clever way, and no doubt it does work, but it does not solve
>>the original question not in a micron :)
>>
>>The whole point is that C6000 does not have an atomic compare-n-swap
>>operation. Generally, it is not doable to obtain an exclusive access to
>>memory in a multitasking mode for the C6000 architecture.
>>
>>Any solution is a variant of the exact algorithm: 1) disable interrupts
>>2) do whatever is needed in the single process mode and 3) restore
>>interrupts.
>>
>>It really does not matter how interrupts were disabled, the point is
>>that they were disabled.
>>
>>So I wouldn't try do it in the complicated way of nesting several branches,
>>which is from my point of view an error-prone programming, instead I would
>>use the plain way of changing GIE. OTH, it is completely valid method to
>>do something during branch delay slots, no doubts.
>>
>>I feel uncomfortable that I used so many imperative statements in my email,
>>but only for the purpose of clarifying this interesting problem. Please
>>accept my apologies.
>>
>>Rgds,
>>
>>Andrew
>