Subversion Repositories HelenOS

Rev

Rev 2082 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2082 Rev 2089
1
/*
1
/*
2
 * Copyright (c) 2005 Jakub Jermar
2
 * Copyright (c) 2005 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup ia64   
29
/** @addtogroup ia64   
30
 * @{
30
 * @{
31
 */
31
 */
32
/** @file
32
/** @file
33
 */
33
 */
34
 
34
 
35
#include <arch/ski/ski.h>
35
#include <arch/ski/ski.h>
36
#include <console/console.h>
36
#include <console/console.h>
37
#include <console/chardev.h>
37
#include <console/chardev.h>
38
#include <arch/interrupt.h>
38
#include <arch/interrupt.h>
39
#include <sysinfo/sysinfo.h>
39
#include <sysinfo/sysinfo.h>
40
#include <arch/types.h>
40
#include <arch/types.h>
41
#include <typedefs.h>
-
 
42
#include <ddi/device.h>
41
#include <ddi/device.h>
43
#include <ddi/irq.h>
42
#include <ddi/irq.h>
44
#include <ipc/irq.h>
43
#include <ipc/irq.h>
45
#include <proc/thread.h>
44
#include <proc/thread.h>
46
#include <synch/spinlock.h>
45
#include <synch/spinlock.h>
47
#include <arch/asm.h>
46
#include <arch/asm.h>
48
 
47
 
49
#define SKI_KBD_INR 0
48
#define SKI_KBD_INR 0
50
 
49
 
51
static irq_t ski_kbd_irq;
50
static irq_t ski_kbd_irq;
52
static devno_t ski_kbd_devno;
51
static devno_t ski_kbd_devno;
53
 
52
 
54
chardev_t ski_console;
53
chardev_t ski_console;
55
chardev_t ski_uconsole;
54
chardev_t ski_uconsole;
56
 
55
 
57
static bool kbd_disabled;
56
static bool kbd_disabled;
58
 
57
 
59
static void ski_putchar(chardev_t *d, const char ch);
58
static void ski_putchar(chardev_t *d, const char ch);
60
static int32_t ski_getchar(void);
59
static int32_t ski_getchar(void);
61
 
60
 
62
/** Display character on debug console
61
/** Display character on debug console
63
 *
62
 *
64
 * Use SSC (Simulator System Call) to
63
 * Use SSC (Simulator System Call) to
65
 * display character on debug console.
64
 * display character on debug console.
66
 *
65
 *
67
 * @param d Character device.
66
 * @param d Character device.
68
 * @param ch Character to be printed.
67
 * @param ch Character to be printed.
69
 */
68
 */
70
void ski_putchar(chardev_t *d, const char ch)
69
void ski_putchar(chardev_t *d, const char ch)
71
{
70
{
72
    asm volatile (
71
    asm volatile (
73
        "mov r15 = %0\n"
72
        "mov r15 = %0\n"
74
        "mov r32 = %1\n"    /* r32 is in0 */
73
        "mov r32 = %1\n"    /* r32 is in0 */
75
        "break 0x80000\n"   /* modifies r8 */
74
        "break 0x80000\n"   /* modifies r8 */
76
        :
75
        :
77
        : "i" (SKI_PUTCHAR), "r" (ch)
76
        : "i" (SKI_PUTCHAR), "r" (ch)
78
        : "r15", "in0", "r8"
77
        : "r15", "in0", "r8"
79
    );
78
    );
80
   
79
   
81
    if (ch == '\n')
80
    if (ch == '\n')
82
        ski_putchar(d, '\r');
81
        ski_putchar(d, '\r');
83
}
82
}
84
 
83
 
85
/** Ask debug console if a key was pressed.
84
/** Ask debug console if a key was pressed.
86
 *
85
 *
87
 * Use SSC (Simulator System Call) to
86
 * Use SSC (Simulator System Call) to
88
 * get character from debug console.
87
 * get character from debug console.
89
 *
88
 *
90
 * This call is non-blocking.
89
 * This call is non-blocking.
91
 *
90
 *
92
 * @return ASCII code of pressed key or 0 if no key pressed.
91
 * @return ASCII code of pressed key or 0 if no key pressed.
93
 */
92
 */
94
int32_t ski_getchar(void)
93
int32_t ski_getchar(void)
95
{
94
{
96
    uint64_t ch;
95
    uint64_t ch;
97
   
96
   
98
    asm volatile (
97
    asm volatile (
99
        "mov r15 = %1\n"
98
        "mov r15 = %1\n"
100
        "break 0x80000;;\n" /* modifies r8 */
99
        "break 0x80000;;\n" /* modifies r8 */
101
        "mov %0 = r8;;\n"      
100
        "mov %0 = r8;;\n"      
102
 
101
 
103
        : "=r" (ch)
102
        : "=r" (ch)
104
        : "i" (SKI_GETCHAR)
103
        : "i" (SKI_GETCHAR)
105
        : "r15", "r8"
104
        : "r15", "r8"
106
    );
105
    );
107
 
106
 
108
    return (int32_t) ch;
107
    return (int32_t) ch;
109
}
108
}
110
 
109
 
111
/**
110
/**
112
 * This is a blocking wrapper for ski_getchar().
111
 * This is a blocking wrapper for ski_getchar().
113
 * To be used when the kernel crashes.
112
 * To be used when the kernel crashes.
114
 */
113
 */
115
static char ski_getchar_blocking(chardev_t *d)
114
static char ski_getchar_blocking(chardev_t *d)
116
{
115
{
117
    int ch;
116
    int ch;
118
 
117
 
119
    while(!(ch = ski_getchar()))
118
    while(!(ch = ski_getchar()))
120
        ;
119
        ;
121
    if(ch == '\r')
120
    if(ch == '\r')
122
        ch = '\n';
121
        ch = '\n';
123
    return (char) ch;
122
    return (char) ch;
124
}
123
}
125
 
124
 
126
/** Ask keyboard if a key was pressed. */
125
/** Ask keyboard if a key was pressed. */
127
static void poll_keyboard(void)
126
static void poll_keyboard(void)
128
{
127
{
129
    char ch;
128
    char ch;
130
    static char last;
129
    static char last;
131
    ipl_t ipl;
130
    ipl_t ipl;
132
 
131
 
133
    ipl = interrupts_disable();
132
    ipl = interrupts_disable();
134
 
133
 
135
    if (kbd_disabled) {
134
    if (kbd_disabled) {
136
        interrupts_restore(ipl);
135
        interrupts_restore(ipl);
137
        return;
136
        return;
138
    }
137
    }
139
       
138
       
140
    spinlock_lock(&ski_kbd_irq.lock);
139
    spinlock_lock(&ski_kbd_irq.lock);
141
 
140
 
142
    ch = ski_getchar();
141
    ch = ski_getchar();
143
    if(ch == '\r')
142
    if(ch == '\r')
144
        ch = '\n';
143
        ch = '\n';
145
    if (ch) {
144
    if (ch) {
146
        if (ski_kbd_irq.notif_cfg.notify && ski_kbd_irq.notif_cfg.answerbox) {
145
        if (ski_kbd_irq.notif_cfg.notify && ski_kbd_irq.notif_cfg.answerbox) {
147
            chardev_push_character(&ski_uconsole, ch);
146
            chardev_push_character(&ski_uconsole, ch);
148
            ipc_irq_send_notif(&ski_kbd_irq);
147
            ipc_irq_send_notif(&ski_kbd_irq);
149
        } else {
148
        } else {
150
            chardev_push_character(&ski_console, ch);
149
            chardev_push_character(&ski_console, ch);
151
        }  
150
        }  
152
        last = ch;
151
        last = ch;
153
        spinlock_unlock(&ski_kbd_irq.lock);
152
        spinlock_unlock(&ski_kbd_irq.lock);
154
        interrupts_restore(ipl);
153
        interrupts_restore(ipl);
155
        return;
154
        return;
156
    }
155
    }
157
 
156
 
158
    if (last) {
157
    if (last) {
159
        if (ski_kbd_irq.notif_cfg.notify && ski_kbd_irq.notif_cfg.answerbox) {
158
        if (ski_kbd_irq.notif_cfg.notify && ski_kbd_irq.notif_cfg.answerbox) {
160
            chardev_push_character(&ski_uconsole, 0);
159
            chardev_push_character(&ski_uconsole, 0);
161
            ipc_irq_send_notif(&ski_kbd_irq);
160
            ipc_irq_send_notif(&ski_kbd_irq);
162
        }
161
        }
163
        last = 0;
162
        last = 0;
164
    }
163
    }
165
 
164
 
166
    spinlock_unlock(&ski_kbd_irq.lock);
165
    spinlock_unlock(&ski_kbd_irq.lock);
167
    interrupts_restore(ipl);
166
    interrupts_restore(ipl);
168
}
167
}
169
 
168
 
170
/* Called from getc(). */
169
/* Called from getc(). */
171
static void ski_kbd_enable(chardev_t *d)
170
static void ski_kbd_enable(chardev_t *d)
172
{
171
{
173
    kbd_disabled = false;
172
    kbd_disabled = false;
174
}
173
}
175
 
174
 
176
/* Called from getc(). */
175
/* Called from getc(). */
177
static void ski_kbd_disable(chardev_t *d)
176
static void ski_kbd_disable(chardev_t *d)
178
{
177
{
179
    kbd_disabled = true;   
178
    kbd_disabled = true;   
180
}
179
}
181
 
180
 
182
/** Decline to service hardware IRQ.
181
/** Decline to service hardware IRQ.
183
 *
182
 *
184
 * This is only a virtual IRQ, so always decline.
183
 * This is only a virtual IRQ, so always decline.
185
 *
184
 *
186
 * @return Always IRQ_DECLINE.
185
 * @return Always IRQ_DECLINE.
187
 */
186
 */
188
static irq_ownership_t ski_kbd_claim(void)
187
static irq_ownership_t ski_kbd_claim(void)
189
{
188
{
190
    return IRQ_DECLINE;
189
    return IRQ_DECLINE;
191
}
190
}
192
 
191
 
193
static chardev_operations_t ski_ops = {
192
static chardev_operations_t ski_ops = {
194
    .resume = ski_kbd_enable,
193
    .resume = ski_kbd_enable,
195
    .suspend = ski_kbd_disable,
194
    .suspend = ski_kbd_disable,
196
    .write = ski_putchar,
195
    .write = ski_putchar,
197
    .read = ski_getchar_blocking
196
    .read = ski_getchar_blocking
198
};
197
};
199
 
198
 
200
/** Initialize debug console
199
/** Initialize debug console
201
 *
200
 *
202
 * Issue SSC (Simulator System Call) to
201
 * Issue SSC (Simulator System Call) to
203
 * to open debug console.
202
 * to open debug console.
204
 */
203
 */
205
void ski_init_console(void)
204
void ski_init_console(void)
206
{
205
{
207
    asm volatile (
206
    asm volatile (
208
        "mov r15 = %0\n"
207
        "mov r15 = %0\n"
209
        "break 0x80000\n"
208
        "break 0x80000\n"
210
        :
209
        :
211
        : "i" (SKI_INIT_CONSOLE)
210
        : "i" (SKI_INIT_CONSOLE)
212
        : "r15", "r8"
211
        : "r15", "r8"
213
    );
212
    );
214
 
213
 
215
    chardev_initialize("ski_console", &ski_console, &ski_ops);
214
    chardev_initialize("ski_console", &ski_console, &ski_ops);
216
    chardev_initialize("ski_uconsole", &ski_uconsole, &ski_ops);
215
    chardev_initialize("ski_uconsole", &ski_uconsole, &ski_ops);
217
    stdin = &ski_console;
216
    stdin = &ski_console;
218
    stdout = &ski_console;
217
    stdout = &ski_console;
219
 
218
 
220
    ski_kbd_devno = device_assign_devno();
219
    ski_kbd_devno = device_assign_devno();
221
   
220
   
222
    irq_initialize(&ski_kbd_irq);
221
    irq_initialize(&ski_kbd_irq);
223
    ski_kbd_irq.inr = SKI_KBD_INR;
222
    ski_kbd_irq.inr = SKI_KBD_INR;
224
    ski_kbd_irq.devno = ski_kbd_devno;
223
    ski_kbd_irq.devno = ski_kbd_devno;
225
    ski_kbd_irq.claim = ski_kbd_claim;
224
    ski_kbd_irq.claim = ski_kbd_claim;
226
    irq_register(&ski_kbd_irq);
225
    irq_register(&ski_kbd_irq);
227
 
226
 
228
    sysinfo_set_item_val("kbd", NULL, true);
227
    sysinfo_set_item_val("kbd", NULL, true);
229
    sysinfo_set_item_val("kbd.inr", NULL, SKI_KBD_INR);
228
    sysinfo_set_item_val("kbd.inr", NULL, SKI_KBD_INR);
230
    sysinfo_set_item_val("kbd.devno", NULL, ski_kbd_devno);
229
    sysinfo_set_item_val("kbd.devno", NULL, ski_kbd_devno);
231
}
230
}
232
 
231
 
233
void ski_kbd_grab(void)
232
void ski_kbd_grab(void)
234
{
233
{
235
    ipl_t ipl = interrupts_disable();
234
    ipl_t ipl = interrupts_disable();
236
    spinlock_lock(&ski_kbd_irq.lock);
235
    spinlock_lock(&ski_kbd_irq.lock);
237
    ski_kbd_irq.notif_cfg.notify = false;
236
    ski_kbd_irq.notif_cfg.notify = false;
238
    spinlock_unlock(&ski_kbd_irq.lock);
237
    spinlock_unlock(&ski_kbd_irq.lock);
239
    interrupts_restore(ipl);
238
    interrupts_restore(ipl);
240
}
239
}
241
 
240
 
242
void ski_kbd_release(void)
241
void ski_kbd_release(void)
243
{
242
{
244
    ipl_t ipl = interrupts_disable();
243
    ipl_t ipl = interrupts_disable();
245
    spinlock_lock(&ski_kbd_irq.lock);
244
    spinlock_lock(&ski_kbd_irq.lock);
246
    if (ski_kbd_irq.notif_cfg.answerbox)
245
    if (ski_kbd_irq.notif_cfg.answerbox)
247
        ski_kbd_irq.notif_cfg.notify = true;
246
        ski_kbd_irq.notif_cfg.notify = true;
248
    spinlock_unlock(&ski_kbd_irq.lock);
247
    spinlock_unlock(&ski_kbd_irq.lock);
249
    interrupts_restore(ipl);
248
    interrupts_restore(ipl);
250
}
249
}
251
 
250
 
252
 
251
 
253
#define POLL_INTERVAL       50000       /* 50 ms */
252
#define POLL_INTERVAL       50000       /* 50 ms */
254
 
253
 
255
/** Kernel thread for polling keyboard. */
254
/** Kernel thread for polling keyboard. */
256
void kkbdpoll(void *arg)
255
void kkbdpoll(void *arg)
257
{
256
{
258
    while (1) {
257
    while (1) {
259
        poll_keyboard();
258
        poll_keyboard();
260
        thread_usleep(POLL_INTERVAL);
259
        thread_usleep(POLL_INTERVAL);
261
    }
260
    }
262
}
261
}
263
 
262
 
264
/** @}
263
/** @}
265
 */
264
 */
266
 
265