[444] | 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 timer start and alloc library services. */ |
---|
| 32 | #include <spu_timer.h> |
---|
| 33 | #include "spu_timer_internal.h" |
---|
| 34 | |
---|
| 35 | /* The timers. */ |
---|
| 36 | spu_timer_t __spu_timers[SPU_TIMER_NTIMERS] __attribute__ ((aligned (16))); |
---|
| 37 | |
---|
| 38 | /* Active timer list. */ |
---|
| 39 | spu_timer_t *__spu_timers_active; |
---|
| 40 | |
---|
| 41 | /* Stopped (allocated) timer list. */ |
---|
| 42 | spu_timer_t *__spu_timers_stopped; |
---|
| 43 | |
---|
| 44 | /* List of timers being handled. */ |
---|
| 45 | spu_timer_t *__spu_timers_handled; |
---|
| 46 | |
---|
| 47 | /* Bitmask of available timers. */ |
---|
| 48 | unsigned __spu_timers_avail = |
---|
| 49 | ((1 << (SPU_TIMER_NTIMERS - 1)) + ((1 << (SPU_TIMER_NTIMERS - 1)) - 1)); |
---|
| 50 | |
---|
| 51 | /* Allocates an SPU interval timer and returns the timer ID. Must be called |
---|
| 52 | before starting a timer. interval specifies the expiration interval in |
---|
| 53 | timebase units. func specifies the function pointer to expiration handler. |
---|
| 54 | Returns the timer ID on success or <0 on Failure: |
---|
| 55 | * SPU_TIMER_ERR_NONE_FREE - no free timers to allocate |
---|
| 56 | * SPU_TIMER_ERR_INVALID_PARM - invalid parm */ |
---|
| 57 | int |
---|
| 58 | spu_timer_alloc (int interval, void (*func) (int)) |
---|
| 59 | { |
---|
| 60 | unsigned was_enabled; |
---|
| 61 | int id; |
---|
| 62 | if (interval < MIN_INTVL || interval > MAX_INTVL || func == NULL) |
---|
| 63 | return SPU_TIMER_ERR_INVALID_PARM; |
---|
| 64 | |
---|
| 65 | was_enabled = spu_readch (SPU_RdMachStat) & 0x1; |
---|
| 66 | |
---|
| 67 | /* Get id of next available timer. */ |
---|
| 68 | id = spu_extract ((spu_sub ((unsigned) 31, |
---|
| 69 | spu_cntlz (spu_promote |
---|
| 70 | (__spu_timers_avail, 0)))), 0); |
---|
| 71 | |
---|
| 72 | /* No timers avail. */ |
---|
| 73 | if (id == -1) |
---|
| 74 | return SPU_TIMER_ERR_NONE_FREE; |
---|
| 75 | |
---|
| 76 | /* Higher order bits represent lower timer ids. */ |
---|
| 77 | __spu_timers_avail &= ~(1 << (id)); |
---|
| 78 | id = (SPU_TIMER_NTIMERS - 1) - id; |
---|
| 79 | |
---|
| 80 | /* Initialize timer and put it on stopped list. */ |
---|
| 81 | (__spu_timers + id)->func = func; |
---|
| 82 | (__spu_timers + id)->intvl = interval; |
---|
| 83 | (__spu_timers + id)->id = id; |
---|
| 84 | (__spu_timers + id)->state = SPU_TIMER_STOPPED; |
---|
| 85 | |
---|
| 86 | spu_idisable (); |
---|
| 87 | (__spu_timers + id)->next = __spu_timers_stopped; |
---|
| 88 | __spu_timers_stopped = &__spu_timers[id]; |
---|
| 89 | |
---|
| 90 | if (__likely (was_enabled)) |
---|
| 91 | spu_ienable (); |
---|
| 92 | return id; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | /* External interface for starting a timer. See description of |
---|
| 96 | __spu_timer_start(). Returns 0 on success and <0 on failure: |
---|
| 97 | * SPU_TIMER_ERR_INVALID_ID - invalid id |
---|
| 98 | * SPU_TIMER_ERR_NOCLOCK - clock is off |
---|
| 99 | * SPU_TIMER_ERR_NOT_STOPPED - timer not in stopped state */ |
---|
| 100 | int |
---|
| 101 | spu_timer_start (int id) |
---|
| 102 | { |
---|
| 103 | if (id < 0 || id >= SPU_TIMER_NTIMERS) |
---|
| 104 | return SPU_TIMER_ERR_INVALID_ID; |
---|
| 105 | |
---|
| 106 | if (__spu_clock_startcnt == 0) |
---|
| 107 | return SPU_TIMER_ERR_NOCLOCK; |
---|
| 108 | |
---|
| 109 | if (__spu_timers[id].state != SPU_TIMER_STOPPED) |
---|
| 110 | return SPU_TIMER_ERR_NOT_STOPPED; |
---|
| 111 | |
---|
| 112 | __spu_timer_start (id, 1); |
---|
| 113 | |
---|
| 114 | return 0; |
---|
| 115 | } |
---|