Subversion Repositories shark

Rev

Rev 699 | Rev 755 | 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 capacity;
62
 
522 mauro 63
  struct timespec act_time;
64
 
511 giacomo 65
  int avail;
66
 
528 giacomo 67
  struct timespec replenish_expires;
511 giacomo 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;
96
 
97
  switch (proc_table[INTDRIVE_task].status) {
98
 
99
    case INTDRIVE_IDLE:
100
      if (lev->act_number) {
101
        proc_table[INTDRIVE_task].status = INTDRIVE_READY;
102
        event_need_reschedule();
103
      } else {
104
        proc_table[INTDRIVE_task].status = INTDRIVE_WAIT;
105
      }
106
      break;
107
 
108
  }
109
 
110
}
111
 
112
 
113
static void INTDRIVE_wcet_timer(void *arg)
114
{
115
 
116
  INTDRIVE_level_des *lev = (INTDRIVE_level_des *)(arg);
117
 
118
  lev->wcet_timer = NIL;
119
 
120
  kern_raise(XWCET_VIOLATION,INTDRIVE_task);
121
 
122
}
123
 
124
static PID INTDRIVE_public_scheduler(LEVEL l)
125
{
754 giacomo 126
 
127
  if (INTDRIVE_task == NIL) return NIL;
511 giacomo 128
 
129
  if (proc_table[INTDRIVE_task].status == INTDRIVE_READY ||
130
        proc_table[INTDRIVE_task].status == EXE)
131
    return INTDRIVE_task;
132
  else
133
    return NIL;
134
 
135
}
136
 
137
static int INTDRIVE_public_create(LEVEL l, PID p, TASK_MODEL *m)
138
{
139
 
140
  HARD_TASK_MODEL *h;
141
 
142
  if (m->pclass != HARD_PCLASS) return -1;
143
  if (m->level != 0 && m->level != l) return -1;
144
  h = (HARD_TASK_MODEL *)m;
145
  if (!h->wcet && h->periodicity != INTDRIVE) return -1;
146
 
147
  if (INTDRIVE_task != NIL) return -1;
148
 
149
  INTDRIVE_task = p;
150
 
151
  proc_table[INTDRIVE_task].wcet = h->wcet;
152
  proc_table[INTDRIVE_task].status = INTDRIVE_WAIT;
153
  proc_table[INTDRIVE_task].control &= ~CONTROL_CAP;
154
 
155
  return 0;
156
 
157
}
158
 
159
static void INTDRIVE_public_dispatch(LEVEL l, PID p, int nostop)
160
{
161
 
162
  INTDRIVE_level_des *lev = (INTDRIVE_level_des *)(level_table[l]);
163
  struct timespec time;
522 mauro 164
 
165
  kern_gettime(&(lev->act_time));
166
  TIMESPEC_ASSIGN(&time,&(lev->act_time));
511 giacomo 167
  ADDUSEC2TIMESPEC(proc_table[INTDRIVE_task].wcet,&time);
168
 
699 giacomo 169
  if (lev->flags == INTDRIVE_CHECK_WCET)
170
    lev->wcet_timer = kern_event_post(&time,INTDRIVE_wcet_timer,(void *)lev);  
171
 
511 giacomo 172
}
173
 
174
static void INTDRIVE_public_epilogue(LEVEL l, PID p)
175
{
176
 
699 giacomo 177
  struct timespec time;
511 giacomo 178
 
179
  INTDRIVE_level_des *lev = (INTDRIVE_level_des *)(level_table[l]);
180
 
181
  if (lev->wcet_timer != NIL)
182
    kern_event_delete(lev->wcet_timer);
183
 
699 giacomo 184
  SUBTIMESPEC(&schedule_time, &(lev->act_time), &time);
528 giacomo 185
  lev->avail -= TIMESPEC2USEC(&time);
511 giacomo 186
 
187
}
188
 
657 anton 189
static void INTDRIVE_public_activate(LEVEL l, PID p, struct timespec *t)
511 giacomo 190
{
191
 
192
  INTDRIVE_level_des *lev = (INTDRIVE_level_des *)(level_table[l]);
193
 
194
  if (proc_table[INTDRIVE_task].status == INTDRIVE_WAIT) {
543 giacomo 195
 
511 giacomo 196
    proc_table[INTDRIVE_task].status = INTDRIVE_READY;
543 giacomo 197
 
198
    lev->act_number++; 
199
 
511 giacomo 200
  } else {
201
 
202
    if (proc_table[INTDRIVE_task].status == INTDRIVE_IDLE ||
543 giacomo 203
        proc_table[INTDRIVE_task].status == INTDRIVE_READY ||
204
        proc_table[INTDRIVE_task].status == EXE) {
511 giacomo 205
 
206
        #ifdef INTDRIVE_DEBUG
207
          kern_printf("(INTD:WAIT_REC)");
208
        #endif
209
 
210
        lev->act_number++;
211
 
212
    }
213
 
214
  }
215
 
216
  if (lev->replenish_timer == NIL) {
524 giacomo 217
 
528 giacomo 218
    kern_gettime(&(lev->replenish_expires));
219
    ADDUSEC2TIMESPEC(lev->replenish_period,&(lev->replenish_expires));
220
    lev->replenish_timer = kern_event_post(&(lev->replenish_expires),INTDRIVE_timer,(void *)lev);
524 giacomo 221
 
511 giacomo 222
  }
223
 
224
}
225
 
226
static void INTDRIVE_public_unblock(LEVEL l, PID p)
227
{
228
  /* Insert task in the correct position */
229
  proc_table[INTDRIVE_task].status = INTDRIVE_READY;
230
 
231
}
232
 
233
static void INTDRIVE_public_block(LEVEL l, PID p)
234
{
235
 
236
}
237
 
238
static int INTDRIVE_public_message(LEVEL l, PID p, void *m)
239
{
240
  INTDRIVE_level_des *lev = (INTDRIVE_level_des *)(level_table[l]);
698 giacomo 241
  struct timespec time, acttime;
528 giacomo 242
  int temp,delta_time;;
511 giacomo 243
 
543 giacomo 244
  lev->act_number--;
245
 
511 giacomo 246
  if (lev->wcet_timer != NIL)
247
    kern_event_delete(lev->wcet_timer);
248
 
698 giacomo 249
  kern_gettime(&acttime);
250
  SUBTIMESPEC(&acttime, &(lev->act_time), &time);
528 giacomo 251
  lev->avail -= TIMESPEC2USEC(&time);
511 giacomo 252
 
253
  #ifdef INTDRIVE_DEBUG
254
    kern_printf("(INTD:AV:%d)",(int)(lev->avail));
255
  #endif
256
 
257
  if (lev->avail < 0) {
258
    proc_table[INTDRIVE_task].status = INTDRIVE_IDLE;
528 giacomo 259
    temp = -lev->avail;
260
    mul32div32to32(temp,lev->replenish_period,lev->capacity,delta_time);
511 giacomo 261
 
262
    if (lev->replenish_timer != NIL)
263
      kern_event_delete(lev->replenish_timer);
264
 
528 giacomo 265
    ADDUSEC2TIMESPEC(delta_time,&(lev->replenish_expires));                                                                      lev->replenish_timer = kern_event_post(&(lev->replenish_expires),INTDRIVE_timer,(void *)lev);
511 giacomo 266
 
267
    #ifdef INTDRIVE_DEBUG
528 giacomo 268
      kern_printf("(INTD:IDLE:%d)",delta_time);
511 giacomo 269
    #endif
270
 
271
  } else {
272
    if (lev->act_number) {
273
      proc_table[INTDRIVE_task].status = INTDRIVE_READY;
274
 
275
      #ifdef INTDRIVE_DEBUG
276
        kern_printf("(INTD:NEXT_ACT)");
277
      #endif
278
 
279
    } else {
280
 
281
      #ifdef INTDRIVE_DEBUG
282
        kern_printf("(INTD:WAIT_ACT)");
283
      #endif
284
 
285
      proc_table[INTDRIVE_task].status = INTDRIVE_WAIT;
286
 
287
    }
288
  }
289
 
290
  return 0;
522 mauro 291
 
511 giacomo 292
}
293
 
294
static void INTDRIVE_public_end(LEVEL l, PID p)
295
{
296
 
297
  INTDRIVE_level_des *lev = (INTDRIVE_level_des *)(level_table[l]);
298
 
299
  if (lev->replenish_timer != NIL)
300
    kern_event_delete(lev->replenish_timer);
301
 
524 giacomo 302
  if (lev->wcet_timer != NIL)
303
    kern_event_delete(lev->wcet_timer);
304
 
511 giacomo 305
  proc_table[INTDRIVE_task].status = INTDRIVE_IDLE;
306
 
307
}
308
 
309
/* Registration functions */
310
 
311
/*+ Registration function: +*/
312
LEVEL INTDRIVE_register_level(TIME capacity, TIME replenish_period, int flags)
313
{
314
  LEVEL l;            /* the level that we register */
315
  INTDRIVE_level_des *lev;
316
 
317
  printk("INTDRIVE_register_level\n");
318
 
319
  /* request an entry in the level_table */
320
  l = level_alloc_descriptor(sizeof(INTDRIVE_level_des));
321
 
322
  lev = (INTDRIVE_level_des *)level_table[l];
323
 
324
  lev->l.public_scheduler = INTDRIVE_public_scheduler;
325
  lev->l.public_guarantee = NULL;
326
  lev->l.public_create    = INTDRIVE_public_create;
327
  lev->l.public_end       = INTDRIVE_public_end;
328
  lev->l.public_dispatch  = INTDRIVE_public_dispatch;
329
  lev->l.public_epilogue  = INTDRIVE_public_epilogue;
330
  lev->l.public_activate  = INTDRIVE_public_activate;
331
  lev->l.public_unblock   = INTDRIVE_public_unblock;
332
  lev->l.public_block     = INTDRIVE_public_block;
333
  lev->l.public_message   = INTDRIVE_public_message;
334
 
522 mauro 335
  NULL_TIMESPEC(&(lev->act_time));
336
 
511 giacomo 337
  lev->capacity = capacity;
338
  lev->replenish_period = 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