source: trunk/libs/newlib/src/newlib/libc/machine/spu/spu_timer_svcs.c @ 444

Last change on this file since 444 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 3.9 KB
Line 
1/*
2(C) Copyright IBM Corp. 2008
3
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met:
8
9* Redistributions of source code must retain the above copyright notice,
10this list of conditions and the following disclaimer.
11* Redistributions in binary form must reproduce the above copyright
12notice, this list of conditions and the following disclaimer in the
13documentation and/or other materials provided with the distribution.
14* Neither the name of IBM nor the names of its contributors may be
15used to endorse or promote products derived from this software without
16specific prior written permission.
17
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28POSSIBILITY 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.  */
36spu_timer_t __spu_timers[SPU_TIMER_NTIMERS] __attribute__ ((aligned (16)));
37
38/* Active timer list.  */
39spu_timer_t *__spu_timers_active;
40
41/* Stopped (allocated) timer list.  */
42spu_timer_t *__spu_timers_stopped;
43
44/* List of timers being handled.  */
45spu_timer_t *__spu_timers_handled;
46
47/* Bitmask of available timers.  */
48unsigned __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  */
57int
58spu_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  */
100int
101spu_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}
Note: See TracBrowser for help on using the repository browser.