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