Subversion Repositories shark

Rev

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