1 | /* |
---|
2 | (C) Copyright IBM Corp. 2008 |
---|
3 | |
---|
4 | All rights reserved. |
---|
5 | |
---|
6 | Redistribution and use in source and binary forms, with or without |
---|
7 | modification, are permitted provided that the following conditions are met: |
---|
8 | |
---|
9 | * Redistributions of source code must retain the above copyright notice, |
---|
10 | this list of conditions and the following disclaimer. |
---|
11 | * Redistributions in binary form must reproduce the above copyright |
---|
12 | notice, this list of conditions and the following disclaimer in the |
---|
13 | documentation and/or other materials provided with the distribution. |
---|
14 | * Neither the name of IBM nor the names of its contributors may be |
---|
15 | used to endorse or promote products derived from this software without |
---|
16 | specific prior written permission. |
---|
17 | |
---|
18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
21 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
22 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
23 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
24 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
25 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
26 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
27 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
28 | POSSIBILITY OF SUCH DAMAGE. |
---|
29 | */ |
---|
30 | |
---|
31 | /* SPU clock start and read library services. */ |
---|
32 | #include <spu_timer.h> |
---|
33 | #include "spu_timer_internal.h" |
---|
34 | |
---|
35 | /* The software managed timebase value. */ |
---|
36 | volatile uint64_t __spu_tb_val __attribute__ ((aligned (16))); |
---|
37 | |
---|
38 | /* Timeout value of the current interval. */ |
---|
39 | volatile int __spu_tb_timeout __attribute__ ((aligned (16))); |
---|
40 | |
---|
41 | /* Clock start count (clock is running if >0). */ |
---|
42 | volatile unsigned __spu_clock_startcnt __attribute__ ((aligned (16))); |
---|
43 | |
---|
44 | /* Saved interrupt state from clock_start. */ |
---|
45 | volatile unsigned __spu_clock_state_was_enabled; |
---|
46 | |
---|
47 | /* Initializes the software managed timebase, enables the decrementer event, |
---|
48 | starts the decrementer and enables interrupts. Must be called before |
---|
49 | clock or timer services can be used. Should only be called by base app/lib |
---|
50 | code (not from an interrupt/timer handler). |
---|
51 | Returns with interrupts ENABLED. */ |
---|
52 | void |
---|
53 | spu_clock_start (void) |
---|
54 | { |
---|
55 | /* Increment clock start and return if it was already running. */ |
---|
56 | if (++__spu_clock_startcnt > 1) |
---|
57 | return; |
---|
58 | |
---|
59 | __spu_clock_state_was_enabled = spu_readch (SPU_RdMachStat) & 0x1; |
---|
60 | |
---|
61 | spu_idisable (); |
---|
62 | __spu_tb_timeout = CLOCK_START_VALUE; |
---|
63 | __spu_tb_val = 0; |
---|
64 | |
---|
65 | /* Disable, write, enable the decrementer. */ |
---|
66 | __enable_spu_decr (__spu_tb_timeout, __disable_spu_decr ()); |
---|
67 | |
---|
68 | spu_ienable (); |
---|
69 | |
---|
70 | return; |
---|
71 | } |
---|
72 | |
---|
73 | /* Returns a monotonically increasing, 64-bit counter, in timebase units, |
---|
74 | relative to the last call to spu_clock_start(). */ |
---|
75 | uint64_t |
---|
76 | spu_clock_read (void) |
---|
77 | { |
---|
78 | int64_t time; |
---|
79 | unsigned was_enabled; |
---|
80 | |
---|
81 | /* Return 0 if clock is off. */ |
---|
82 | if (__spu_clock_startcnt == 0) |
---|
83 | return 0LL; |
---|
84 | |
---|
85 | was_enabled = spu_readch (SPU_RdMachStat) & 0x1; |
---|
86 | spu_idisable (); |
---|
87 | |
---|
88 | time = __spu_tb_val + (__spu_tb_timeout - spu_readch (SPU_RdDec)); |
---|
89 | |
---|
90 | if (__likely (was_enabled)) |
---|
91 | spu_ienable (); |
---|
92 | return time; |
---|
93 | } |
---|