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

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [conts/] [baremetal/] [kmi_service/] [main.c] - Blame information for rev 5

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

Line No. Rev Author Line
1 2 drasko
/*
2
 * Keyboard and Mouse service for userspace
3
 */
4
#include <l4lib/lib/addr.h>
5
#include <l4lib/lib/thread.h>
6
#include <l4lib/irq.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 <keyboard.h>
17
#include <mouse.h>
18
 
19
#define KEYBOARDS_TOTAL         1
20
#define MOUSE_TOTAL             1
21
 
22
static struct capability caparray[32];
23
static int total_caps = 0;
24
 
25
struct keyboard kbd[KEYBOARDS_TOTAL];
26
struct mouse mouse[MOUSE_TOTAL];
27
 
28
int cap_read_all()
29
{
30
        int ncaps;
31
        int err;
32
 
33
        /* Read number of capabilities */
34
        if ((err = l4_capability_control(CAP_CONTROL_NCAPS,
35
                                         0, &ncaps)) < 0) {
36
                printf("l4_capability_control() reading # of"
37
                       " capabilities failed.\n Could not "
38
                       "complete CAP_CONTROL_NCAPS request.\n");
39
                BUG();
40
        }
41
        total_caps = ncaps;
42
 
43
        /* Read all capabilities */
44
        if ((err = l4_capability_control(CAP_CONTROL_READ,
45
                                         0, caparray)) < 0) {
46
                printf("l4_capability_control() reading of "
47
                       "capabilities failed.\n Could not "
48
                       "complete CAP_CONTROL_READ_CAPS request.\n");
49
                BUG();
50
        }
51
        return 0;
52
}
53
 
54
int cap_share_all_with_space()
55
{
56
        int err;
57
 
58
        /* Share all capabilities */
59
        if ((err = l4_capability_control(CAP_CONTROL_SHARE,
60
                                         CAP_SHARE_ALL_SPACE, 0)) < 0) {
61
                printf("l4_capability_control() sharing of "
62
                       "capabilities failed.\n Could not "
63
                       "complete CAP_CONTROL_SHARE request. err=%d\n",
64
                       err);
65
                BUG();
66
        }
67
 
68
        return 0;
69
}
70
 
71
/*
72
 * Scans for up to KEYBOARDS_TOTAL
73
 * keyboard devices and MOUSE_TOTAL mouse
74
 * in capabilities.
75
 */
76
int kmi_probe_devices(void)
77
{
78
        int keyboards = 0, nmouse = 0;
79
 
80
        /* Scan for timer devices */
81
        for (int i = 0; i < total_caps; i++) {
82
                /* Match device type */
83
                if (cap_devtype(&caparray[i]) == CAP_DEVTYPE_KEYBOARD) {
84
                        /* Copy to correct device index */
85
                        memcpy(&kbd[cap_devnum(&caparray[i])].cap,
86
                               &caparray[i], sizeof(kbd[0].cap));
87
                        keyboards++;
88
                }
89
                if (cap_devtype(&caparray[i]) == CAP_DEVTYPE_MOUSE) {
90
                        /* Copy to correct device index */
91
                        memcpy(&mouse[cap_devnum(&caparray[i])].cap,
92
                               &caparray[i], sizeof(mouse[0].cap));
93
                        nmouse++;
94
                }
95
 
96
        }
97
 
98
        if (keyboards != KEYBOARDS_TOTAL) {
99
                printf("%s: Error, not all keyboards could be found. "
100
                       "keyboards=%d\n", __CONTAINER_NAME__, keyboards);
101
                return -ENODEV;
102
        }
103
        if (nmouse != MOUSE_TOTAL) {
104
                printf("%s: Error, not all mouse could be found. "
105
                       "mouse=%d\n", __CONTAINER_NAME__, nmouse);
106
                return -ENODEV;
107
        }
108
 
109
        return 0;
110
}
111
 
112
int keyboard_irq_handler(void *arg)
113
{
114
        int err;
115
        struct keyboard *keyboard = (struct keyboard *)arg;
116
        const int slot = 0;
117
 
118
        /*
119
         * For versatile, KMI refernce clock = 24MHz
120
         * KMI manual says we need 8MHz clock,
121
         * so divide by 3
122
         */
123
        kmi_keyboard_init(keyboard->base, 3);
124
        printf("%s: Keyboard initialization done..\n", __CONTAINER_NAME__);
125
 
126
        /* Register self for timer irq, using notify slot 0 */
127
        if ((err = l4_irq_control(IRQ_CONTROL_REGISTER, slot,
128
                                  keyboard->cap.irq)) < 0) {
129
                printf("%s: FATAL: Keyboard irq could not be registered. "
130
                       "err=%d\n", __FUNCTION__, err);
131
                BUG();
132
        }
133
 
134
        /* Handle irqs forever */
135
        while (1) {
136
                char c;
137
 
138
                /* Block on irq */
139
                int data = l4_irq_wait(slot, keyboard->cap.irq);
140
                while (data--)
141
                        if ((c = kmi_keyboard_read(keyboard->base, &keyboard->state)))
142
                                printf("%c", c);
143
 
144
                /*
145
                 * Kernel has disabled irq for keyboard
146
                 * We need to enable it
147
                 */
148
                kmi_rx_irq_enable(keyboard->base);
149
        }
150
}
151
 
152
int mouse_irq_handler(void *arg)
153
{
154
        int err;
155
        struct mouse *mouse = (struct mouse *)arg;
156
        const int slot = 0;
157
 
158
        /*
159
         * For versatile, KMI refernce clock = 24MHz
160
         * KMI manual says we need 8MHz clock,
161
         * so divide by 3
162
         */
163
        kmi_mouse_init(mouse->base, 3);
164
        printf("%s: Mouse initialization done..\n", __CONTAINER_NAME__);
165
 
166
        /* Register self for timer irq, using notify slot 0 */
167
        if ((err = l4_irq_control(IRQ_CONTROL_REGISTER, slot,
168
                                  mouse->cap.irq)) < 0) {
169
                printf("%s: FATAL: Mouse irq could not be registered. "
170
                       "err=%d\n", __FUNCTION__, err);
171
                BUG();
172
        }
173
 
174
        /* Handle irqs forever */
175
        while (1) {
176
                int c;
177
 
178
                /* Block on irq */
179
                int data = l4_irq_wait(slot, mouse->cap.irq);
180
                while (data--)
181
                        if ((c = kmi_data_read(mouse->base)))
182
                                printf("mouse data: %d\n", c);
183
 
184
                /*
185
                 * Kernel has disabled irq for mouse
186
                 * We need to enable it
187
                 */
188
                kmi_rx_irq_enable(mouse->base);
189
        }
190
}
191
 
192
int kmi_setup_devices(void)
193
{
194
        struct l4_thread thread;
195
        struct l4_thread *tptr = &thread;
196
        int err;
197
 
198
        for (int i = 0; i < KEYBOARDS_TOTAL; i++) {
199
                /* Get one page from address pool */
200
                kbd[i].base = (unsigned long)l4_new_virtual(1);
201
                kbd[i].state.shift = 0;
202
                kbd[i].state.caps_lock = 0;
203
                kbd[i].state.keyup = 0;
204
 
205
                /* Map timer to a virtual address region */
206
                if (IS_ERR(l4_map((void *)__pfn_to_addr(kbd[i].cap.start),
207
                                  (void *)kbd[i].base, kbd[i].cap.size,
208
                                  MAP_USR_IO, self_tid()))) {
209
                        printf("%s: FATAL: Failed to map Keyboard device "
210
                               "%d to a virtual address\n",
211
                               __CONTAINER_NAME__,
212
                               cap_devnum(&kbd[i].cap));
213
                        BUG();
214
                }
215
 
216
                /*
217
                 * Create new keyboard irq handler thread.
218
                 *
219
                 * This will initialize its keyboard argument, register
220
                 * itself as its irq handler, initiate keyboard and
221
                 * wait on irqs.
222
                 */
223
                if ((err = thread_create(keyboard_irq_handler, &kbd[i],
224
                                         TC_SHARE_SPACE,
225
                                         &tptr)) < 0) {
226
                        printf("FATAL: Creation of irq handler "
227
                               "thread failed.\n");
228
                        BUG();
229
                }
230
        }
231
 
232
        for (int i = 0; i < MOUSE_TOTAL; i++) {
233
                /* Get one page from address pool */
234
                mouse[i].base = (unsigned long)l4_new_virtual(1);
235
 
236
                /* Map timer to a virtual address region */
237
                if (IS_ERR(l4_map((void *)__pfn_to_addr(mouse[i].cap.start),
238
                           (void *)mouse[i].base, mouse[i].cap.size,
239
                           MAP_USR_IO, self_tid()))) {
240
                        printf("%s: FATAL: Failed to map Mouse device "
241
                               "%d to a virtual address\n",
242
                               __CONTAINER_NAME__, cap_devnum(&mouse[i].cap));
243
                        BUG();
244
                }
245
 
246
                /*
247
                 * Create new mouse irq handler thread.
248
                 *
249
                 * This will initialize its mouse argument, register
250
                 * itself as its irq handler, initiate mouse and
251
                 * wait on irqs.
252
                 */
253
                if ((err = thread_create(mouse_irq_handler, &mouse[i],
254
                                         TC_SHARE_SPACE, &tptr)) < 0) {
255
                        printf("FATAL: Creation of irq handler "
256
                               "thread failed.\n");
257
                        BUG();
258
                }
259
        }
260
 
261
        return 0;
262
}
263
 
264
/*
265
 * Declare a statically allocated char buffer
266
 * with enough bitmap size to cover given size
267
 */
268
#define DECLARE_IDPOOL(name, size)      \
269
char name[(sizeof(struct id_pool) + ((size >> 12) >> 3))]
270
 
271
#define PAGE_POOL_SIZE                  SZ_1MB
272
static struct address_pool device_vaddr_pool;
273
DECLARE_IDPOOL(device_id_pool, PAGE_POOL_SIZE);
274
 
275
/*
276
 * Initialize a virtual address pool
277
 * for mapping physical devices.
278
 */
279
void init_vaddr_pool(void)
280
{
281
        for (int i = 0; i < total_caps; i++) {
282
                /* Find the virtual memory region for this process */
283
                if (cap_type(&caparray[i]) == CAP_TYPE_MAP_VIRTMEM
284
                    && __pfn_to_addr(caparray[i].start) ==
285
                    (unsigned long)vma_start) {
286
 
287
                        /*
288
                         * Do we have any unused virtual space
289
                         * where we run, and do we have enough
290
                         * pages of it to map all timers?
291
                         */
292
                        if (__pfn(page_align_up(__end)) + KEYBOARDS_TOTAL +
293
                            MOUSE_TOTAL <= caparray[i].end) {
294
                                /*
295
                                 * Yes. We initialize the device
296
                                 * virtual memory pool here.
297
                                 *
298
                                 * We may allocate virtual memory
299
                                 * addresses from this pool.
300
                                 */
301
                                address_pool_init(&device_vaddr_pool,
302
                                                  (struct id_pool *)&device_id_pool,
303
                                                  page_align_up(__end),
304
                                                  __pfn_to_addr(caparray[i].end));
305
                                return;
306
                        } else
307
                                goto out_err;
308
                }
309
        }
310
 
311
out_err:
312
        printf("%s: FATAL: No virtual memory "
313
               "region available to map "
314
               "devices.\n", __CONTAINER_NAME__);
315
        BUG();
316
}
317
 
318
void *l4_new_virtual(int npages)
319
{
320
        return address_new(&device_vaddr_pool, npages, PAGE_SIZE);
321
}
322
 
323
void handle_requests(void)
324
{
325
        u32 mr[MR_UNUSED_TOTAL];
326
        l4id_t senderid;
327
        u32 tag;
328
        int ret;
329
 
330
        printf("%s: Initiating ipc.\n", __CONTAINER__);
331
        if ((ret = l4_receive(L4_ANYTHREAD)) < 0) {
332
                printf("%s: %s: IPC Error: %d. Quitting...\n", __CONTAINER__,
333
                       __FUNCTION__, ret);
334
                BUG();
335
        }
336
 
337
        /* Syslib conventional ipc data which uses first few mrs. */
338
        tag = l4_get_tag();
339
        senderid = l4_get_sender();
340
 
341
        /* Read mrs not used by syslib */
342
        for (int i = 0; i < MR_UNUSED_TOTAL; i++)
343
                mr[i] = read_mr(MR_UNUSED_START + i);
344
 
345
        /*
346
         * TODO:
347
         *
348
         * Maybe add tags here that handle requests for sharing
349
         * of the requested timer device with the client?
350
         *
351
         * In order to be able to do that, we should have a
352
         * shareable/grantable capability to the device. Also
353
         * the request should (currently) come from a task
354
         * inside the current container
355
         */
356
        switch (tag) {
357
        default:
358
                printf("%s: Error received ipc from 0x%x residing "
359
                       "in container %x with an unrecognized tag: "
360
                       "0x%x\n", __CONTAINER__, senderid,
361
                       __cid(senderid), tag);
362
        }
363
 
364
        /* Reply */
365
        if ((ret = l4_ipc_return(ret)) < 0) {
366
                printf("%s: IPC return error: %d.\n", __FUNCTION__, ret);
367
                BUG();
368
        }
369
}
370
 
371
/*
372
 * UTCB-size aligned utcb.
373
 *
374
 * BIG WARNING NOTE: This declaration is legal if we are
375
 * running in a disjoint virtual address space, where the
376
 * utcb declaration lies in a unique virtual address in
377
 * the system.
378
 */
379
#define DECLARE_UTCB(name) \
380
        struct utcb name ALIGN(sizeof(struct utcb))
381
 
382
DECLARE_UTCB(utcb);
383
 
384
/* Set up own utcb for ipc */
385
int l4_utcb_setup(void *utcb_address)
386
{
387
        struct task_ids ids;
388
        struct exregs_data exregs;
389
        int err;
390
 
391
        l4_getid(&ids);
392
 
393
        /* Clear utcb */
394
        memset(utcb_address, 0, sizeof(struct utcb));
395
 
396
        /* Setup exregs for utcb request */
397
        memset(&exregs, 0, sizeof(exregs));
398
        exregs_set_utcb(&exregs, (unsigned long)utcb_address);
399
 
400
        if ((err = l4_exchange_registers(&exregs, ids.tid)) < 0)
401
                return err;
402
 
403
        return 0;
404
}
405
 
406
void main(void)
407
{
408
        int err;
409
 
410
        /* Read all capabilities */
411
        cap_read_all();
412
 
413
        /* Share all with space */
414
        cap_share_all_with_space();
415
 
416
        /* Scan for keyboard devices in capabilities */
417
        kmi_probe_devices();
418
 
419
        /* Initialize virtual address pool for timers */
420
        init_vaddr_pool();
421
 
422
        /* Setup own static utcb */
423
        if ((err = l4_utcb_setup(&utcb)) < 0) {
424
                printf("FATAL: Could not set up own utcb. "
425
                       "err=%d\n", err);
426
                BUG();
427
        }
428
 
429
        /* Map and initialize keyboard devices */
430
        kmi_setup_devices();
431
 
432
        /* Listen for timer requests */
433
        while (1)
434
                handle_requests();
435
}
436
 
437
 

powered by: WebSVN 2.1.0

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