OpenCores
URL https://opencores.org/ocsvn/c0or1k/c0or1k/trunk

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [conts/] [baremetal/] [timer_service/] [main.c] - Blame information for rev 6

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 drasko
/*
2
 * Timer service for userspace
3
 */
4
#include <l4lib/lib/addr.h>
5
#include <l4lib/irq.h>
6
#include <l4lib/lib/thread.h>
7
#include <l4lib/ipcdefs.h>
8
#include <l4/api/errno.h>
9
#include <l4/api/irq.h>
10
#include <l4/api/capability.h>
11
#include <l4/generic/cap-types.h>
12
#include <l4/api/space.h>
13
#include <malloc/malloc.h>
14
#include <container.h>
15
#include <linker.h>
16
#include <timer.h>
17
#include <libdev/timer.h>
18
 
19
/* Capabilities of this service */
20
static struct capability caparray[32];
21
static int total_caps = 0;
22
 
23
/* Total number of timer chips being handled by us */
24
#define TIMERS_TOTAL            1
25
static struct timer global_timer[TIMERS_TOTAL];
26
 
27
/* Deafult timer to be used for sleep/wake etc purposes */
28
#define SLEEP_WAKE_TIMER        0
29
 
30
/* tasks whose sleep time has finished */
31
struct wake_task_list wake_tasks;
32
 
33
/* tid of handle_request thread */
34
l4id_t tid_ipc_handler;
35
 
36
int cap_read_all()
37
{
38
        int ncaps;
39
        int err;
40
 
41
        /* Read number of capabilities */
42
        if ((err = l4_capability_control(CAP_CONTROL_NCAPS,
43
                                         0, &ncaps)) < 0) {
44
                printf("l4_capability_control() reading # of"
45
                       " capabilities failed.\n Could not "
46
                       "complete CAP_CONTROL_NCAPS request.\n");
47
                BUG();
48
        }
49
        total_caps = ncaps;
50
 
51
        /* Read all capabilities */
52
        if ((err = l4_capability_control(CAP_CONTROL_READ,
53
                                         0, caparray)) < 0) {
54
                printf("l4_capability_control() reading of "
55
                       "capabilities failed.\n Could not "
56
                       "complete CAP_CONTROL_READ_CAPS request.\n");
57
                BUG();
58
        }
59
 
60
        return 0;
61
}
62
 
63
int cap_share_all_with_space()
64
{
65
        int err;
66
 
67
        /* Share all capabilities */
68
        if ((err = l4_capability_control(CAP_CONTROL_SHARE,
69
                                         CAP_SHARE_ALL_SPACE, 0)) < 0) {
70
                printf("l4_capability_control() sharing of "
71
                       "capabilities failed.\n Could not "
72
                       "complete CAP_CONTROL_SHARE request. err=%d\n",
73
                       err);
74
                BUG();
75
        }
76
 
77
        return 0;
78
}
79
 
80
/*
81
 * Initialize timer devices
82
 */
83
void timer_struct_init(struct timer* timer, unsigned long base)
84
{
85
        timer->base = base;
86
        timer->count = 0;
87
        timer->slot = 0;
88
        l4_mutex_init(&timer->task_list_lock);
89
 
90
        for (int i = 0; i < BUCKET_BASE_LEVEL_SIZE ; ++i) {
91
                link_init(&timer->task_list.bucket_level0[i]);
92
        }
93
 
94
        for (int i = 0; i < BUCKET_HIGHER_LEVEL_SIZE ; ++i) {
95
                link_init(&timer->task_list.bucket_level1[i]);
96
                link_init(&timer->task_list.bucket_level2[i]);
97
                link_init(&timer->task_list.bucket_level3[i]);
98
                link_init(&timer->task_list.bucket_level4[i]);
99
        }
100
}
101
 
102
/*
103
 * Initialize wake list head structure
104
 */
105
void wake_task_list_init(void)
106
{
107
        link_init(&wake_tasks.head);
108
        wake_tasks.end = &wake_tasks.head;
109
        l4_mutex_init(&wake_tasks.wake_list_lock);
110
}
111
 
112
/*
113
 * Allocate new sleeper task struct
114
 */
115
struct sleeper_task *new_sleeper_task(l4id_t tid, int ret)
116
{
117
        struct sleeper_task *task;
118
 
119
        /* May be we can prepare a cache for timer_task structs */
120
        task = (struct sleeper_task *)kzalloc(sizeof(struct sleeper_task));
121
 
122
        link_init(&task->list);
123
        task->tid = tid;
124
        task->retval = ret;
125
 
126
        return task;
127
}
128
 
129
void free_sleeper_task(struct sleeper_task *task)
130
{
131
        kfree(task);
132
        task = NULL;
133
}
134
 
135
/*
136
 * Find the bucket list correspongding to seconds value
137
 */
138
struct link* find_bucket_list(unsigned long seconds)
139
{
140
        struct link *vector;
141
        struct sleeper_task_bucket *bucket;
142
 
143
        bucket = &global_timer[SLEEP_WAKE_TIMER].task_list;
144
 
145
        /*
146
         * TODO: Check if we have already surpassed seconds
147
         */
148
        if (IS_IN_LEVEL0_BUCKET(seconds)) {
149
                vector = &bucket->bucket_level0[GET_BUCKET_LEVEL0(seconds)];
150
        } else if (IS_IN_LEVEL1_BUCKET(seconds)) {
151
                vector = &bucket->bucket_level1[GET_BUCKET_LEVEL1(seconds)];
152
        } else if (IS_IN_LEVEL2_BUCKET(seconds)) {
153
                vector = &bucket->bucket_level2[GET_BUCKET_LEVEL2(seconds)];
154
        } else if (IS_IN_LEVEL3_BUCKET(seconds)) {
155
                vector = &bucket->bucket_level3[GET_BUCKET_LEVEL3(seconds)];
156
        } else {
157
                vector = &bucket->bucket_level4[GET_BUCKET_LEVEL4(seconds)];
158
        }
159
 
160
        return vector;
161
}
162
 
163
/*
164
 * Scans for up to TIMERS_TOTAL timer devices in capabilities.
165
 */
166
int timer_probe_devices(void)
167
{
168
        int timers = 0;
169
 
170
        /* Scan for timer devices */
171
        for (int i = 0; i < total_caps; i++) {
172
                /* Match device type */
173
                if (cap_devtype(&caparray[i]) == CAP_DEVTYPE_TIMER) {
174
                        /* Copy to correct device index */
175
                        memcpy(&global_timer[cap_devnum(&caparray[i]) - 1].cap,
176
                               &caparray[i], sizeof(global_timer[0].cap));
177
                        timers++;
178
                }
179
        }
180
 
181
        if (timers != TIMERS_TOTAL) {
182
                printf("%s: Error, not all timers could be found. "
183
                       "timers=%d\n", __CONTAINER_NAME__, timers);
184
                return -ENODEV;
185
        }
186
        return 0;
187
}
188
 
189
/*
190
 * Irq handler for timer interrupts
191
 */
192
int timer_irq_handler(void *arg)
193
{
194
        int err;
195
        struct timer *timer = (struct timer *)arg;
196
        struct link *vector;
197
        const int slot = 0;
198
 
199
        /*
200
          * Initialise timer
201
          * 1 interrupt per second
202
          */
203
        timer_init(timer->base, 1000000);
204
 
205
        /* Register self for timer irq, using notify slot 0 */
206
        if ((err = l4_irq_control(IRQ_CONTROL_REGISTER, slot,
207
                                  timer->cap.irq)) < 0) {
208
                printf("%s: FATAL: Timer irq could not be registered. "
209
                       "err=%d\n", __FUNCTION__, err);
210
                BUG();
211
        }
212
 
213
        /* Enable Timer */
214
        timer_start(timer->base);
215
 
216
        /* Handle irqs forever */
217
        while (1) {
218
                int count;
219
                struct link *task_list;
220
 
221
                /* Block on irq */
222
                if((count = l4_irq_wait(slot, timer->cap.irq)) < 0) {
223
                        printf("l4_irq_wait() returned with negative value\n");
224
                        BUG();
225
                }
226
 
227
                /*
228
                  * Update timer count
229
                  * TODO: Overflow check, we have 1 interrupt/sec from timer
230
                  * with 32bit count it will take 9years to overflow
231
                  */
232
                timer->count += count;
233
                printf("Got timer irq, current count = 0x%x\n", timer->count);
234
 
235
                /* find bucket list of taks to be woken for current count */
236
                vector = find_bucket_list(timer->count);
237
 
238
                if (!list_empty(vector)) {
239
                        /* Removing tasks from sleeper list */
240
                        l4_mutex_lock(&global_timer[SLEEP_WAKE_TIMER].task_list_lock);
241
                        task_list = list_detach(vector);
242
                        l4_mutex_unlock(&global_timer[SLEEP_WAKE_TIMER].task_list_lock);
243
 
244
                        /* Add tasks to wake_task_list */
245
                        l4_mutex_lock(&wake_tasks.wake_list_lock);
246
                        list_attach(task_list, &wake_tasks.head, wake_tasks.end);
247
                        l4_mutex_unlock(&wake_tasks.wake_list_lock);
248
 
249
                        /*
250
                         * Send ipc to handle_request
251
                         * thread to send wake signals
252
                         */
253
                        l4_send(tid_ipc_handler,L4_IPC_TAG_TIMER_WAKE_THREADS);
254
                }
255
        }
256
}
257
 
258
/*
259
 * Helper routine to wake tasks from wake list
260
 */
261
void task_wake(void)
262
{
263
        struct sleeper_task *struct_ptr, *temp_ptr;
264
        int ret;
265
 
266
        if (!list_empty(&wake_tasks.head)) {
267
                list_foreach_removable_struct(struct_ptr, temp_ptr,
268
                                              &wake_tasks.head, list) {
269
                        /* Remove task from wake list */
270
                        l4_mutex_lock(&wake_tasks.wake_list_lock);
271
                        list_remove(&struct_ptr->list);
272
                        l4_mutex_unlock(&wake_tasks.wake_list_lock);
273
 
274
                        /* Set sender correctly */
275
                        l4_set_sender(struct_ptr->tid);
276
 
277
                        printf("%s : Waking thread 0x%x at time 0x%x\n", __CONTAINER_NAME__,
278
                                    struct_ptr->tid, global_timer[SLEEP_WAKE_TIMER].count);
279
 
280
                        /* send wake ipc */
281
                        if ((ret = l4_ipc_return(struct_ptr->retval)) < 0) {
282
                                printf("%s: IPC return error: %d.\n",
283
                                       __FUNCTION__, ret);
284
                                BUG();
285
                        }
286
 
287
                        /* free allocated sleeper task struct */
288
                        free_sleeper_task(struct_ptr);
289
                }
290
        }
291
        /* If wake list is empty set end = start */
292
        if (list_empty(&wake_tasks.head))
293
                wake_tasks.end = &wake_tasks.head;
294
 
295
}
296
 
297
int timer_setup_devices(void)
298
{
299
        struct l4_thread thread;
300
        struct l4_thread *tptr = &thread;
301
        int err;
302
 
303
        for (int i = 0; i < TIMERS_TOTAL; i++) {
304
                /* initialize timer */
305
                timer_struct_init(&global_timer[i],(unsigned long)l4_new_virtual(1) );
306
 
307
                /* Map timer to a virtual address region */
308
                if (IS_ERR(l4_map((void *)__pfn_to_addr(global_timer[i].cap.start),
309
                                  (void *)global_timer[i].base, global_timer[i].cap.size,
310
                                  MAP_USR_IO,
311
                                  self_tid()))) {
312
                        printf("%s: FATAL: Failed to map TIMER device "
313
                               "%d to a virtual address\n",
314
                               __CONTAINER_NAME__,
315
                               cap_devnum(&global_timer[i].cap));
316
                        BUG();
317
                }
318
 
319
                /*
320
                 * Create new timer irq handler thread.
321
                 *
322
                 * This will initialize its timer argument, register
323
                 * itself as its irq handler, initiate the timer and
324
                 * wait on irqs.
325
                 */
326
                if ((err = thread_create(timer_irq_handler, &global_timer[i],
327
                                         TC_SHARE_SPACE,
328
                                         &tptr)) < 0) {
329
                        printf("FATAL: Creation of irq handler "
330
                               "thread failed.\n");
331
                        BUG();
332
                }
333
        }
334
 
335
        return 0;
336
}
337
 
338
/*
339
 * Declare a statically allocated char buffer
340
 * with enough bitmap size to cover given size
341
 */
342
#define DECLARE_IDPOOL(name, size)      \
343
         char name[(sizeof(struct id_pool) + ((size >> 12) >> 3))]
344
 
345
#define PAGE_POOL_SIZE                  SZ_1MB
346
static struct address_pool device_vaddr_pool;
347
DECLARE_IDPOOL(device_id_pool, PAGE_POOL_SIZE);
348
 
349
/*
350
 * Initialize a virtual address pool
351
 * for mapping physical devices.
352
 */
353
void init_vaddr_pool(void)
354
{
355
        for (int i = 0; i < total_caps; i++) {
356
                /* Find the virtual memory region for this process */
357
                if (cap_type(&caparray[i]) == CAP_TYPE_MAP_VIRTMEM
358
                    && __pfn_to_addr(caparray[i].start) ==
359
                    (unsigned long)vma_start) {
360
 
361
                        /*
362
                         * Do we have any unused virtual space
363
                         * where we run, and do we have enough
364
                         * pages of it to map all timers?
365
                         */
366
                        if (__pfn(page_align_up(__end))
367
                            + TIMERS_TOTAL <= caparray[i].end) {
368
                                /*
369
                                 * Yes. We initialize the device
370
                                 * virtual memory pool here.
371
                                 *
372
                                 * We may allocate virtual memory
373
                                 * addresses from this pool.
374
                                 */
375
                                address_pool_init(&device_vaddr_pool,
376
                                                  (struct id_pool *)&device_id_pool,
377
                                                  page_align_up(__end),
378
                                                  __pfn_to_addr(caparray[i].end));
379
                                return;
380
                        } else
381
                                goto out_err;
382
                }
383
        }
384
 
385
out_err:
386
        printf("%s: FATAL: No virtual memory "
387
               "region available to map "
388
               "devices.\n", __CONTAINER_NAME__);
389
        BUG();
390
}
391
 
392
void *l4_new_virtual(int npages)
393
{
394
        return address_new(&device_vaddr_pool, npages, PAGE_SIZE);
395
}
396
 
397
/*
398
 * Got request for sleep for seconds,
399
 * right now max sleep allowed is 2^32 sec
400
 */
401
void task_sleep(l4id_t tid, unsigned long seconds, int ret)
402
{
403
        struct sleeper_task *task = new_sleeper_task(tid, ret);
404
        struct link *vector;
405
 
406
        /* can overflow happen here?, timer is in 32bit mode */
407
        seconds += global_timer[SLEEP_WAKE_TIMER].count;
408
 
409
        printf("sleep wake timer lock is present at address %lx\n",
410
                    ( (unsigned long)&global_timer[SLEEP_WAKE_TIMER].task_list_lock.lock));
411
 
412
        vector = find_bucket_list(seconds);
413
 
414
        printf("Acquiring lock for sleep wake timer\n");
415
        l4_mutex_lock(&global_timer[SLEEP_WAKE_TIMER].task_list_lock);
416
        printf("got lock for sleep wake timer\n");
417
 
418
        list_insert(&task->list, vector);
419
 
420
        printf("Releasing lock for sleep wake timer\n");
421
        l4_mutex_unlock(&global_timer[SLEEP_WAKE_TIMER].task_list_lock);
422
        printf("released lock for sleep wake timer\n");
423
 
424
}
425
 
426
void handle_requests(void)
427
{
428
        u32 mr[MR_UNUSED_TOTAL];
429
        l4id_t senderid;
430
        u32 tag;
431
        int ret;
432
 
433
        if ((ret = l4_receive(L4_ANYTHREAD)) < 0) {
434
                printf("%s: %s: IPC Error: %d. Quitting...\n",
435
                       __CONTAINER__, __FUNCTION__, ret);
436
                BUG();
437
        }
438
 
439
        /* Syslib conventional ipc data which uses first few mrs. */
440
        tag = l4_get_tag();
441
        senderid = l4_get_sender();
442
 
443
        /* Read mrs not used by syslib */
444
        for (int i = 0; i < MR_UNUSED_TOTAL; i++)
445
                mr[i] = read_mr(MR_UNUSED_START + i);
446
 
447
        /*
448
         * TODO:
449
         *
450
         * Maybe add tags here that handle requests for sharing
451
         * of the requested timer device with the client?
452
         *
453
         * In order to be able to do that, we should have a
454
         * shareable/grantable capability to the device. Also
455
         * the request should (currently) come from a task
456
         * inside the current container
457
         */
458
        switch (tag) {
459
        /* Return time in seconds, since the timer was started */
460
        case L4_IPC_TAG_TIMER_GETTIME:
461
                printf("%s: Got get time request from thread 0x%x "
462
                            " at time = 0x%x\n", __CONTAINER_NAME__,
463
                            senderid, global_timer[SLEEP_WAKE_TIMER].count);
464
 
465
                write_mr(2, global_timer[SLEEP_WAKE_TIMER].count);
466
 
467
                /* Reply */
468
                if ((ret = l4_ipc_return(ret)) < 0) {
469
                        printf("%s: IPC return error: %d.\n", __FUNCTION__, ret);
470
                        BUG();
471
                }
472
                break;
473
 
474
        case L4_IPC_TAG_TIMER_SLEEP:
475
                printf("%s: Got sleep request from thread 0x%x "
476
                            "for 0x%x seconds at 0x%x seconds\n",
477
                            __CONTAINER_NAME__, senderid, mr[0],
478
                            global_timer[SLEEP_WAKE_TIMER].count);
479
 
480
                if (mr[0] > 0) {
481
                        task_sleep(senderid, mr[0], ret);
482
                }
483
                else {
484
                        if ((ret = l4_ipc_return(ret)) < 0) {
485
                                printf("%s: IPC return error: %d.\n",
486
                                       __FUNCTION__, ret);
487
                                BUG();
488
                        }
489
                }
490
                break;
491
 
492
        /* Intra container ipc by irq_thread */
493
        case L4_IPC_TAG_TIMER_WAKE_THREADS:
494
                task_wake();
495
                break;
496
 
497
        default:
498
                printf("%s: Error received ipc from 0x%x residing "
499
                       "in container %x with an unrecognized tag: "
500
                       "0x%x\n", __CONTAINER__, senderid,
501
                       __cid(senderid), tag);
502
        }
503
}
504
 
505
/*
506
 * UTCB-size aligned utcb.
507
 *
508
 * BIG WARNING NOTE: This declaration is legal if we are
509
 * running in a disjoint virtual address space, where the
510
 * utcb declaration lies in a unique virtual address in
511
 * the system.
512
 */
513
#define DECLARE_UTCB(name) \
514
        struct utcb name ALIGN(sizeof(struct utcb))
515
 
516
DECLARE_UTCB(utcb);
517
 
518
/* Set up own utcb for ipc */
519
int l4_utcb_setup(void *utcb_address)
520
{
521
        struct task_ids ids;
522
        struct exregs_data exregs;
523
        int err;
524
 
525
        l4_getid(&ids);
526
 
527
        /* Clear utcb */
528
        memset(utcb_address, 0, sizeof(struct utcb));
529
 
530
        /* Setup exregs for utcb request */
531
        memset(&exregs, 0, sizeof(exregs));
532
        exregs_set_utcb(&exregs, (unsigned long)utcb_address);
533
 
534
        if ((err = l4_exchange_registers(&exregs, ids.tid)) < 0)
535
                return err;
536
 
537
        return 0;
538
}
539
 
540
void main(void)
541
{
542
        int err;
543
 
544
        /* Read all capabilities */
545
        cap_read_all();
546
 
547
        /* Share all with space */
548
        cap_share_all_with_space();
549
 
550
        /* Scan for timer devices in capabilities */
551
        timer_probe_devices();
552
 
553
        /* Initialize virtual address pool for timers */
554
        init_vaddr_pool();
555
 
556
        /* Setup own static utcb */
557
        if ((err = l4_utcb_setup(&utcb)) < 0) {
558
                printf("FATAL: Could not set up own utcb. "
559
                       "err=%d\n", err);
560
                BUG();
561
        }
562
 
563
        /* initialise timed_out_task list */
564
        wake_task_list_init();
565
 
566
        /* Map and initialize timer devices */
567
        timer_setup_devices();
568
 
569
        /* Set the tid of ipc handler */
570
        tid_ipc_handler = self_tid();
571
 
572
        /* Listen for timer requests */
573
        while (1)
574
                handle_requests();
575
}
576
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.