Rev 522 | Rev 528 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
511 | giacomo | 1 | /* |
2 | * Project: S.Ha.R.K. |
||
3 | * |
||
4 | * Coordinators: |
||
5 | * Giorgio Buttazzo <giorgio@sssup.it> |
||
6 | * Paolo Gai <pj@gandalf.sssup.it> |
||
7 | * |
||
8 | * Authors : |
||
9 | * Giacomo Guidi <giacomo@gandalf.sssup.it> |
||
10 | * (see the web pages for full authors list) |
||
11 | * |
||
12 | * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy) |
||
13 | * |
||
14 | * http://www.sssup.it |
||
15 | * http://retis.sssup.it |
||
16 | * http://shark.sssup.it |
||
17 | */ |
||
18 | |||
19 | /* |
||
20 | * Copyright (C) 2000,2002 Paolo Gai |
||
21 | * |
||
22 | * This program is free software; you can redistribute it and/or modify |
||
23 | * it under the terms of the GNU General Public License as published by |
||
24 | * the Free Software Foundation; either version 2 of the License, or |
||
25 | * (at your option) any later version. |
||
26 | * |
||
27 | * This program is distributed in the hope that it will be useful, |
||
28 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
30 | * GNU General Public License for more details. |
||
31 | * |
||
32 | * You should have received a copy of the GNU General Public License |
||
33 | * along with this program; if not, write to the Free Software |
||
34 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
35 | * |
||
36 | */ |
||
37 | |||
38 | /* Interrupt Driver Module */ |
||
39 | |||
40 | #include <modules/intdrive.h> |
||
41 | #include <kernel/model.h> |
||
42 | #include <kernel/descr.h> |
||
43 | #include <kernel/var.h> |
||
44 | #include <kernel/func.h> |
||
45 | |||
46 | #include <ll/i386/64bit.h> |
||
47 | |||
48 | /*+ Status used in the level +*/ |
||
49 | #define INTDRIVE_READY MODULE_STATUS_BASE /*+ - Ready status +*/ |
||
50 | #define INTDRIVE_WCET_VIOLATED MODULE_STATUS_BASE+2 /*+ when wcet is finished +*/ |
||
51 | #define INTDRIVE_IDLE MODULE_STATUS_BASE+3 /*+ to wait the replenish +*/ |
||
52 | #define INTDRIVE_WAIT MODULE_STATUS_BASE+4 /*+ to wait the activation */ |
||
53 | |||
54 | //#define INTDRIVE_DEBUG |
||
55 | |||
56 | /*+ the level redefinition for the IntDrive +*/ |
||
57 | typedef struct { |
||
58 | level_des l; /*+ the standard level descriptor +*/ |
||
59 | |||
60 | TIME replenish_period; |
||
61 | TIME next_time; |
||
62 | TIME capacity; |
||
63 | |||
522 | mauro | 64 | struct timespec act_time; |
65 | |||
511 | giacomo | 66 | int avail; |
67 | |||
68 | int replenish_timer; |
||
69 | int wcet_timer; |
||
70 | |||
71 | int act_number; /*+ the activation number +*/ |
||
72 | |||
73 | int flags; /*+ the init flags... +*/ |
||
74 | |||
75 | bandwidth_t U; /*+ the used bandwidth +*/ |
||
76 | |||
77 | } INTDRIVE_level_des; |
||
78 | |||
79 | PID INTDRIVE_task = NIL; |
||
80 | |||
81 | /* Replenish the capacity */ |
||
82 | static void INTDRIVE_timer(void *arg) |
||
83 | { |
||
84 | |||
85 | INTDRIVE_level_des *lev = (INTDRIVE_level_des *)(arg); |
||
86 | |||
87 | lev->replenish_timer = NIL; |
||
88 | |||
89 | #ifdef INTDRIVE_DEBUG |
||
90 | kern_printf("(INTD:TIMER)"); |
||
91 | #endif |
||
92 | |||
93 | if (INTDRIVE_task == NIL) return; |
||
94 | |||
95 | lev->avail = lev->capacity; |
||
524 | giacomo | 96 | lev->next_time = lev->replenish_period; |
511 | giacomo | 97 | |
98 | switch (proc_table[INTDRIVE_task].status) { |
||
99 | |||
100 | case INTDRIVE_IDLE: |
||
101 | if (lev->act_number) { |
||
102 | proc_table[INTDRIVE_task].status = INTDRIVE_READY; |
||
103 | event_need_reschedule(); |
||
104 | } else { |
||
105 | proc_table[INTDRIVE_task].status = INTDRIVE_WAIT; |
||
106 | } |
||
107 | break; |
||
108 | |||
109 | } |
||
110 | |||
111 | } |
||
112 | |||
113 | |||
114 | static void INTDRIVE_wcet_timer(void *arg) |
||
115 | { |
||
116 | |||
117 | INTDRIVE_level_des *lev = (INTDRIVE_level_des *)(arg); |
||
118 | |||
119 | lev->wcet_timer = NIL; |
||
120 | |||
121 | kern_raise(XWCET_VIOLATION,INTDRIVE_task); |
||
122 | |||
123 | } |
||
124 | |||
125 | static PID INTDRIVE_public_scheduler(LEVEL l) |
||
126 | { |
||
127 | |||
128 | if (proc_table[INTDRIVE_task].status == INTDRIVE_READY || |
||
129 | proc_table[INTDRIVE_task].status == EXE) |
||
130 | return INTDRIVE_task; |
||
131 | else |
||
132 | return NIL; |
||
133 | |||
134 | } |
||
135 | |||
136 | static int INTDRIVE_public_create(LEVEL l, PID p, TASK_MODEL *m) |
||
137 | { |
||
138 | |||
139 | HARD_TASK_MODEL *h; |
||
140 | |||
141 | if (m->pclass != HARD_PCLASS) return -1; |
||
142 | if (m->level != 0 && m->level != l) return -1; |
||
143 | h = (HARD_TASK_MODEL *)m; |
||
144 | if (!h->wcet && h->periodicity != INTDRIVE) return -1; |
||
145 | |||
146 | if (INTDRIVE_task != NIL) return -1; |
||
147 | |||
148 | INTDRIVE_task = p; |
||
149 | |||
150 | proc_table[INTDRIVE_task].wcet = h->wcet; |
||
151 | proc_table[INTDRIVE_task].status = INTDRIVE_WAIT; |
||
152 | proc_table[INTDRIVE_task].control &= ~CONTROL_CAP; |
||
153 | |||
154 | return 0; |
||
155 | |||
156 | } |
||
157 | |||
158 | static void INTDRIVE_public_dispatch(LEVEL l, PID p, int nostop) |
||
159 | { |
||
160 | |||
161 | INTDRIVE_level_des *lev = (INTDRIVE_level_des *)(level_table[l]); |
||
162 | struct timespec time; |
||
522 | mauro | 163 | |
164 | kern_gettime(&(lev->act_time)); |
||
165 | TIMESPEC_ASSIGN(&time,&(lev->act_time)); |
||
511 | giacomo | 166 | ADDUSEC2TIMESPEC(proc_table[INTDRIVE_task].wcet,&time); |
167 | |||
168 | lev->wcet_timer = kern_event_post(&time,INTDRIVE_wcet_timer,(void *)lev); |
||
169 | |||
170 | } |
||
171 | |||
172 | static void INTDRIVE_public_epilogue(LEVEL l, PID p) |
||
173 | { |
||
174 | |||
175 | struct timespec time; |
||
176 | int temp; |
||
177 | |||
178 | INTDRIVE_level_des *lev = (INTDRIVE_level_des *)(level_table[l]); |
||
179 | |||
180 | if (lev->wcet_timer != NIL) |
||
181 | kern_event_delete(lev->wcet_timer); |
||
182 | |||
522 | mauro | 183 | SUBTIMESPEC(&schedule_time, &(lev->act_time), &time); |
511 | giacomo | 184 | temp = TIMESPEC2USEC(&time); |
185 | |||
186 | lev->avail -= temp; |
||
187 | |||
188 | } |
||
189 | |||
190 | static void INTDRIVE_public_activate(LEVEL l, PID p) |
||
191 | { |
||
192 | |||
193 | struct timespec time; |
||
194 | INTDRIVE_level_des *lev = (INTDRIVE_level_des *)(level_table[l]); |
||
195 | |||
196 | if (proc_table[INTDRIVE_task].status == INTDRIVE_WAIT) { |
||
197 | proc_table[INTDRIVE_task].status = INTDRIVE_READY; |
||
198 | } else { |
||
199 | |||
200 | if (proc_table[INTDRIVE_task].status == INTDRIVE_IDLE || |
||
201 | proc_table[INTDRIVE_task].status == INTDRIVE_READY) { |
||
202 | |||
203 | #ifdef INTDRIVE_DEBUG |
||
204 | kern_printf("(INTD:WAIT_REC)"); |
||
205 | #endif |
||
206 | |||
207 | lev->act_number++; |
||
208 | |||
209 | } |
||
210 | |||
211 | } |
||
212 | |||
213 | if (lev->replenish_timer == NIL) { |
||
524 | giacomo | 214 | |
511 | giacomo | 215 | kern_gettime(&time); |
216 | ADDUSEC2TIMESPEC(lev->next_time,&time); |
||
217 | |||
218 | lev->replenish_timer = kern_event_post(&time,INTDRIVE_timer,(void *)lev); |
||
524 | giacomo | 219 | |
511 | giacomo | 220 | } |
221 | |||
222 | } |
||
223 | |||
224 | static void INTDRIVE_public_unblock(LEVEL l, PID p) |
||
225 | { |
||
226 | /* Insert task in the correct position */ |
||
227 | proc_table[INTDRIVE_task].status = INTDRIVE_READY; |
||
228 | |||
229 | } |
||
230 | |||
231 | static void INTDRIVE_public_block(LEVEL l, PID p) |
||
232 | { |
||
233 | |||
234 | } |
||
235 | |||
236 | static int INTDRIVE_public_message(LEVEL l, PID p, void *m) |
||
237 | { |
||
238 | INTDRIVE_level_des *lev = (INTDRIVE_level_des *)(level_table[l]); |
||
239 | struct timespec time; |
||
240 | int temp; |
||
241 | |||
242 | if (lev->wcet_timer != NIL) |
||
243 | kern_event_delete(lev->wcet_timer); |
||
244 | |||
522 | mauro | 245 | SUBTIMESPEC(&schedule_time, &(lev->act_time), &time); |
511 | giacomo | 246 | temp = TIMESPEC2USEC(&time); |
247 | |||
248 | lev->avail -= temp; |
||
249 | #ifdef INTDRIVE_DEBUG |
||
250 | kern_printf("(INTD:AV:%d)",(int)(lev->avail)); |
||
251 | #endif |
||
252 | |||
253 | if (lev->avail < 0) { |
||
254 | proc_table[INTDRIVE_task].status = INTDRIVE_IDLE; |
||
255 | temp = lev->capacity - lev->avail; |
||
256 | mul32div32to32(temp,lev->replenish_period,lev->capacity,lev->next_time); |
||
257 | |||
258 | if (lev->replenish_timer != NIL) |
||
259 | kern_event_delete(lev->replenish_timer); |
||
260 | |||
261 | kern_gettime(&time); |
||
262 | ADDUSEC2TIMESPEC(lev->next_time,&time); |
||
263 | lev->replenish_timer = kern_event_post(&time,INTDRIVE_timer,(void *)lev); |
||
264 | |||
265 | #ifdef INTDRIVE_DEBUG |
||
266 | kern_printf("(INTD:IDLE)"); |
||
267 | #endif |
||
268 | |||
269 | } else { |
||
270 | if (lev->act_number) { |
||
271 | lev->act_number--; |
||
272 | proc_table[INTDRIVE_task].status = INTDRIVE_READY; |
||
273 | |||
274 | #ifdef INTDRIVE_DEBUG |
||
275 | kern_printf("(INTD:NEXT_ACT)"); |
||
276 | #endif |
||
277 | |||
278 | } else { |
||
279 | |||
280 | #ifdef INTDRIVE_DEBUG |
||
281 | kern_printf("(INTD:WAIT_ACT)"); |
||
282 | #endif |
||
283 | |||
284 | proc_table[INTDRIVE_task].status = INTDRIVE_WAIT; |
||
285 | |||
286 | } |
||
287 | } |
||
288 | |||
289 | return 0; |
||
522 | mauro | 290 | |
511 | giacomo | 291 | } |
292 | |||
293 | static void INTDRIVE_public_end(LEVEL l, PID p) |
||
294 | { |
||
295 | |||
296 | INTDRIVE_level_des *lev = (INTDRIVE_level_des *)(level_table[l]); |
||
297 | |||
298 | if (lev->replenish_timer != NIL) |
||
299 | kern_event_delete(lev->replenish_timer); |
||
300 | |||
524 | giacomo | 301 | if (lev->wcet_timer != NIL) |
302 | kern_event_delete(lev->wcet_timer); |
||
303 | |||
511 | giacomo | 304 | proc_table[INTDRIVE_task].status = INTDRIVE_IDLE; |
305 | |||
306 | } |
||
307 | |||
308 | /* Registration functions */ |
||
309 | |||
310 | /*+ Registration function: +*/ |
||
311 | LEVEL INTDRIVE_register_level(TIME capacity, TIME replenish_period, int flags) |
||
312 | { |
||
313 | LEVEL l; /* the level that we register */ |
||
314 | INTDRIVE_level_des *lev; |
||
315 | |||
316 | printk("INTDRIVE_register_level\n"); |
||
317 | |||
318 | /* request an entry in the level_table */ |
||
319 | l = level_alloc_descriptor(sizeof(INTDRIVE_level_des)); |
||
320 | |||
321 | lev = (INTDRIVE_level_des *)level_table[l]; |
||
322 | |||
323 | lev->l.public_scheduler = INTDRIVE_public_scheduler; |
||
324 | lev->l.public_guarantee = NULL; |
||
325 | lev->l.public_create = INTDRIVE_public_create; |
||
326 | lev->l.public_end = INTDRIVE_public_end; |
||
327 | lev->l.public_dispatch = INTDRIVE_public_dispatch; |
||
328 | lev->l.public_epilogue = INTDRIVE_public_epilogue; |
||
329 | lev->l.public_activate = INTDRIVE_public_activate; |
||
330 | lev->l.public_unblock = INTDRIVE_public_unblock; |
||
331 | lev->l.public_block = INTDRIVE_public_block; |
||
332 | lev->l.public_message = INTDRIVE_public_message; |
||
333 | |||
522 | mauro | 334 | NULL_TIMESPEC(&(lev->act_time)); |
335 | |||
511 | giacomo | 336 | lev->capacity = capacity; |
337 | lev->replenish_period = replenish_period; |
||
338 | lev->next_time = replenish_period; |
||
339 | lev->replenish_timer = NIL; |
||
340 | lev->wcet_timer = NIL; |
||
341 | lev->flags = flags; |
||
342 | lev->act_number = 0; |
||
522 | mauro | 343 | lev->avail = capacity; |
511 | giacomo | 344 | mul32div32to32(MAX_BANDWIDTH,lev->capacity,lev->replenish_period,lev->U); |
345 | |||
346 | return l; |
||
347 | } |
||
348 | |||
349 | bandwidth_t INTDRIVE_usedbandwidth(LEVEL l) |
||
350 | { |
||
351 | INTDRIVE_level_des *lev = (INTDRIVE_level_des *)(level_table[l]); |
||
352 | |||
353 | return lev->U; |
||
354 | } |
||
355 |