timerintr.c
4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**************************************************************************
* *
* Copyright (C) 1994, Silicon Graphics, Inc. *
* *
* These coded instructions, statements, and computer programs contain *
* unpublished proprietary information of Silicon Graphics, Inc., and *
* are protected by Federal copyright law. They may not be disclosed *
* to third parties or copied or duplicated in any form, in whole or *
* in part, without the prior written consent of Silicon Graphics, Inc. *
* *
**************************************************************************/
#include <rmon.h>
#include "osint.h"
/*
* Global counter to keep track of elapsed ticks since calling
* TimerServicesInit
*/
OSTime __osCurrentTime;
/*
* Global variable for the count reigster value from last retrace
* interrupt
*/
u32 __osBaseCounter;
u32 __osViIntrCount; /* # of VI interrupts */
/*
* Global variable for the count reigster value from last __osSetTimerIntr
*/
u32 __osTimerCounter;
/* Global double-linked timer list structure */
OSTimer __osBaseTimer;
OSTimer *__osTimerList = &__osBaseTimer;
/*
* Profiling variables, placed in this file so that profile code
* doesn't get sucked in by makerom unnecessarily.
*/
#ifndef _FINALROM
OSMesgQueue __osProfTimerQ;
OSProf *__osProfileList;
OSProf *__osProfileListEnd;
u32 __osProfileOverflowBin;
#endif
/*
* Initialize timer services global data structures
*/
void
__osTimerServicesInit(void)
{
__osCurrentTime = 0; /* Reset the current time counter */
__osBaseCounter = 0;
__osViIntrCount = 0;
/* Initialize the global timer list */
__osTimerList->next = __osTimerList->prev = __osTimerList;
__osTimerList->interval = __osTimerList->value = (OSTime)0;
__osTimerList->mq = (OSMesgQueue *)NULL;
__osTimerList->msg = (OSMesg *)NULL;
}
/*
* Processes the R4300 timer interrupt
*/
void
__osTimerInterrupt(void)
{
OSTimer *t;
u32 count, elapsed_cycles;
#ifndef _FINALROM
/* profiler stuff */
u32 pc;
s32 offset;
OSProf *prof = __osProfileList;
#endif
if (__osTimerList->next == __osTimerList)
/* there is no active timer */
return;
for (;;) {
t = __osTimerList->next;
if (t == __osTimerList) {
/* timer list has been emptied */
__osSetCompare(0);
__osTimerCounter = 0;
break;
}
/* Compute time since timer was set */
count = osGetCount();
elapsed_cycles = count - __osTimerCounter;
/* Have enough ticks gone by since timer was set? */
__osTimerCounter = count;
if ((OSTime)elapsed_cycles < t->value) {
/* not yet ready for next item on list */
t->value -= (OSTime)elapsed_cycles;
__osSetTimerIntr(t->value);
break;
}
/* Remove head of the list and process it */
t->prev->next = t->next;
t->next->prev = t->prev;
t->next = NULL;
t->prev = NULL;
/* Send signal to wake up waiting threads */
if (t->mq != NULL) {
#ifdef _FINALROM
(void)osSendMesg(t->mq, t->msg, OS_MESG_NOBLOCK);
}
#else
if (t->mq != &__osProfTimerQ) {
(void)osSendMesg(t->mq, t->msg, OS_MESG_NOBLOCK);
}
else {
/*
* Determine which bin to put pc in.
*/
pc = __osRunQueue->context.pc;
for (prof = __osProfileList;
prof < __osProfileListEnd; prof++) {
offset = (s32) (pc - (u32)prof->text_start);
if ((offset >= 0) &&
((s32)((u32)(prof->text_end) - pc) > 0)) {
++*((u16 *) ((offset >> 2) + prof->histo_base));
goto __ProfDone;
}
}
++__osProfileOverflowBin;
}
}
__ProfDone:
#endif /* _FINALROM */
/*
* if timer is set up to reload, compute next time timer
* should go off and inster it to the list
*/
if (t->interval) {
t->value = t->interval;
__osInsertTimer(t);
}
}
}
void
__osSetTimerIntr(OSTime tim)
{
OSTime NewTime;
u32 savedMask;
#define WAIT_TIME 10 /* ¿¾¯Ä´À°¤¬É¬Í× */
if( tim < OS_USEC_TO_CYCLES(WAIT_TIME) ) {
tim = OS_USEC_TO_CYCLES(WAIT_TIME);
}
/* disable interrupts */
savedMask = __osDisableInt();
__osTimerCounter = osGetCount();
NewTime = ((OSTime)(__osTimerCounter) + tim);
__osSetCompare((u32)(NewTime));
__osRestoreInt(savedMask);
}
OSTime
__osInsertTimer(OSTimer *t)
{
OSTimer *timep;
OSTime tim;
u32 savedMask;
/* disable interrupts */
savedMask = __osDisableInt();
/* Insert the new timer into the sorted list */
timep = __osTimerList->next;
tim = t->value;
for ( ; (timep != __osTimerList) && (tim > timep->value);
timep = timep->next)
tim -= timep->value;
t->value = tim;
if (timep != __osTimerList)
timep->value -= tim; /* Subtract off tim from the */
/* timer next to the new insted timer */
t->next = timep;
t->prev = timep->prev;
timep->prev->next = t;
timep->prev = t;
__osRestoreInt(savedMask);
return(tim);
}