1 | /*
|
---|
2 | * Copyright (c) 2008 Pavel Rimsky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
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
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
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
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
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
|
---|
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
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup sparc64
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * @brief Niagara input/output driver based on hypervisor calls.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <arch/drivers/niagara.h>
|
---|
38 | #include <console/chardev.h>
|
---|
39 | #include <console/console.h>
|
---|
40 | #include <ddi/ddi.h>
|
---|
41 | #include <ddi/device.h>
|
---|
42 | #include <arch/asm.h>
|
---|
43 | #include <arch.h>
|
---|
44 | #include <mm/slab.h>
|
---|
45 | #include <arch/drivers/kbd.h>
|
---|
46 | #include <arch/sun4v/hypercall.h>
|
---|
47 | #include <sysinfo/sysinfo.h>
|
---|
48 | #include <ipc/irq.h>
|
---|
49 | #include <print.h>
|
---|
50 | #include <proc/thread.h>
|
---|
51 | #include <console/console.h>
|
---|
52 | #include <genarch/srln/srln.h>
|
---|
53 |
|
---|
54 | #define POLL_INTERVAL 10000
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * The driver is polling based, but in order to notify the userspace
|
---|
58 | * of a key being pressed, we need to supply the interface with some
|
---|
59 | * interrupt number. The interrupt number can be arbitrary as it it
|
---|
60 | * will never be used for identifying HW interrupts, but only in
|
---|
61 | * notifying the userspace.
|
---|
62 | */
|
---|
63 | #define FICTIONAL_INR 1
|
---|
64 |
|
---|
65 |
|
---|
66 | static niagara_instance_t *instance = NULL;
|
---|
67 |
|
---|
68 | static void niagara_putchar(outdev_t *, const wchar_t, bool);
|
---|
69 |
|
---|
70 | /** character device operations */
|
---|
71 | static outdev_operations_t niagara_ops = {
|
---|
72 | .write = niagara_putchar,
|
---|
73 | .redraw = NULL
|
---|
74 | };
|
---|
75 |
|
---|
76 | /*
|
---|
77 | * The driver uses hypercalls to print characters to the console. Since the
|
---|
78 | * hypercall cannot be performed from the userspace, we do this:
|
---|
79 | * The kernel "little brother" driver (which will be present no matter what the
|
---|
80 | * DDI architecture is - as we need the kernel part for the kconsole)
|
---|
81 | * defines a shared buffer. Kernel walks through the buffer (in the same thread
|
---|
82 | * which is used for polling the keyboard) and prints any pending characters
|
---|
83 | * to the console (using hypercalls). The userspace fb server maps this shared
|
---|
84 | * buffer to its address space and output operation it does is performed using
|
---|
85 | * the mapped buffer. The shared buffer definition follows.
|
---|
86 | */
|
---|
87 | #define OUTPUT_BUFFER_SIZE ((PAGE_SIZE) - 2 * 8)
|
---|
88 | static volatile struct {
|
---|
89 | uint64_t read_ptr;
|
---|
90 | uint64_t write_ptr;
|
---|
91 | char data[OUTPUT_BUFFER_SIZE];
|
---|
92 | }
|
---|
93 | __attribute__ ((packed))
|
---|
94 | __attribute__ ((aligned(PAGE_SIZE)))
|
---|
95 | output_buffer;
|
---|
96 |
|
---|
97 | #if 0
|
---|
98 | /** Niagara character device */
|
---|
99 | chardev_t niagara_io;
|
---|
100 |
|
---|
101 | /** defined in drivers/kbd.c */
|
---|
102 | extern kbd_type_t kbd_type;
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * The character read will be stored here until the (notified) uspace
|
---|
106 | * driver picks it up.
|
---|
107 | */
|
---|
108 | static char read_char;
|
---|
109 |
|
---|
110 |
|
---|
111 | #endif
|
---|
112 |
|
---|
113 | /** Writes a single character to the standard output. */
|
---|
114 | static inline void do_putchar(const char c) {
|
---|
115 | /* repeat until the buffer is non-full */
|
---|
116 | while (__hypercall_fast1(CONS_PUTCHAR, c) == EWOULDBLOCK)
|
---|
117 | ;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /** Writes a single character to the standard output. */
|
---|
121 | static void niagara_putchar(outdev_t *dev, const wchar_t ch, bool silent)
|
---|
122 | {
|
---|
123 | do_putchar(ch);
|
---|
124 | if (ch == '\n')
|
---|
125 | do_putchar('\r');
|
---|
126 | }
|
---|
127 |
|
---|
128 | #if 0
|
---|
129 | /**
|
---|
130 | * Grabs the input for kernel.
|
---|
131 | */
|
---|
132 | void niagara_grab(void)
|
---|
133 | {
|
---|
134 | ipl_t ipl = interrupts_disable();
|
---|
135 | spinlock_lock(&niagara_irq.lock);
|
---|
136 | niagara_irq.notif_cfg.notify = false;
|
---|
137 | spinlock_unlock(&niagara_irq.lock);
|
---|
138 | interrupts_restore(ipl);
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Releases the input so that userspace can use it.
|
---|
143 | */
|
---|
144 | void niagara_release(void)
|
---|
145 | {
|
---|
146 | ipl_t ipl = interrupts_disable();
|
---|
147 | spinlock_lock(&niagara_irq.lock);
|
---|
148 | if (niagara_irq.notif_cfg.answerbox)
|
---|
149 | niagara_irq.notif_cfg.notify = true;
|
---|
150 | spinlock_unlock(&niagara_irq.lock);
|
---|
151 | interrupts_restore(ipl);
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Default suspend/resume operation for the input device.
|
---|
156 | */
|
---|
157 | static void niagara_noop(chardev_t *d)
|
---|
158 | {
|
---|
159 | }
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Called when actively reading the character.
|
---|
163 | */
|
---|
164 | static char niagara_read(chardev_t *d)
|
---|
165 | {
|
---|
166 | uint64_t c;
|
---|
167 |
|
---|
168 | if (__hypercall_fast_ret1(0, 0, 0, 0, 0, CONS_GETCHAR, &c) == EOK) {
|
---|
169 | return (char) c;
|
---|
170 | }
|
---|
171 |
|
---|
172 | return '\0';
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Returns the character last read. This function is called from the
|
---|
177 | * pseudocode - the character returned by this function is passed to
|
---|
178 | * the userspace keyboard driver.
|
---|
179 | */
|
---|
180 | char niagara_getc(void) {
|
---|
181 | return read_char;
|
---|
182 | }
|
---|
183 |
|
---|
184 | #endif
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Function regularly called by the keyboard polling thread. Asks the
|
---|
188 | * hypervisor whether there is any unread character. If so, it picks it up
|
---|
189 | * and sends it to the upper layers of HelenOS.
|
---|
190 | */
|
---|
191 | static void niagara_poll(niagara_instance_t *instance)
|
---|
192 | {
|
---|
193 | while (output_buffer.read_ptr != output_buffer.write_ptr) {
|
---|
194 | do_putchar(output_buffer.data[output_buffer.read_ptr]);
|
---|
195 | output_buffer.read_ptr =
|
---|
196 | ((output_buffer.read_ptr) + 1) % OUTPUT_BUFFER_SIZE;
|
---|
197 | }
|
---|
198 |
|
---|
199 | uint64_t c;
|
---|
200 |
|
---|
201 | if (__hypercall_fast_ret1(0, 0, 0, 0, 0, CONS_GETCHAR, &c) == EOK) {
|
---|
202 | indev_push_character(instance->srlnin, c);
|
---|
203 | }
|
---|
204 |
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Polling thread function.
|
---|
209 | */
|
---|
210 | static void kniagarapoll(void *instance) {
|
---|
211 | while (true) {
|
---|
212 | //MH
|
---|
213 | //if (!silent)
|
---|
214 | niagara_poll(instance);
|
---|
215 |
|
---|
216 | thread_usleep(POLL_INTERVAL);
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Initializes the input/output subsystem so that the Niagara standard
|
---|
222 | * input/output is used.
|
---|
223 | */
|
---|
224 | static void niagara_init(void)
|
---|
225 | {
|
---|
226 | if (instance)
|
---|
227 | return;
|
---|
228 |
|
---|
229 | instance = malloc(sizeof(niagara_instance_t), FRAME_ATOMIC);
|
---|
230 |
|
---|
231 | if (instance) {
|
---|
232 | instance->thread = thread_create(kniagarapoll, instance, TASK, 0,
|
---|
233 | "kniagarapoll", true);
|
---|
234 |
|
---|
235 | if (!instance->thread) {
|
---|
236 | free(instance);
|
---|
237 | instance = NULL;
|
---|
238 | return;
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | instance->srlnin = NULL;
|
---|
243 | sysinfo_set_item_val("fb.kind", NULL, 5);
|
---|
244 |
|
---|
245 | /*
|
---|
246 | * Set sysinfos and pareas so that the userspace counterpart of the
|
---|
247 | * niagara fb driver can communicate with kernel using a shared buffer.
|
---|
248 | */
|
---|
249 | output_buffer.read_ptr = 0;
|
---|
250 | output_buffer.write_ptr = 0;
|
---|
251 |
|
---|
252 | sysinfo_set_item_val("niagara.outbuf.address", NULL,
|
---|
253 | KA2PA(&output_buffer));
|
---|
254 | sysinfo_set_item_val("niagara.outbuf.size", NULL,
|
---|
255 | PAGE_SIZE);
|
---|
256 | sysinfo_set_item_val("niagara.outbuf.datasize", NULL,
|
---|
257 | OUTPUT_BUFFER_SIZE);
|
---|
258 |
|
---|
259 | static parea_t outbuf_parea;
|
---|
260 | outbuf_parea.pbase = (uintptr_t) (KA2PA(&output_buffer));
|
---|
261 | outbuf_parea.frames = 1;
|
---|
262 | ddi_parea_register(&outbuf_parea);
|
---|
263 |
|
---|
264 | #if 0
|
---|
265 | kbd_type = KBD_SUN4V;
|
---|
266 |
|
---|
267 | devno_t devno = device_assign_devno();
|
---|
268 | irq_initialize(&niagara_irq);
|
---|
269 | niagara_irq.devno = devno;
|
---|
270 | niagara_irq.inr = FICTIONAL_INR;
|
---|
271 | niagara_irq.claim = niagara_claim;
|
---|
272 | niagara_irq.handler = niagara_irq_handler;
|
---|
273 | irq_register(&niagara_irq);
|
---|
274 |
|
---|
275 | sysinfo_set_item_val("kbd", NULL, true);
|
---|
276 | sysinfo_set_item_val("kbd.type", NULL, KBD_SUN4V);
|
---|
277 | sysinfo_set_item_val("kbd.devno", NULL, devno);
|
---|
278 | sysinfo_set_item_val("kbd.inr", NULL, FICTIONAL_INR);
|
---|
279 | #endif
|
---|
280 |
|
---|
281 | /*
|
---|
282 | * Set sysinfos and pareas so that the userspace counterpart of the
|
---|
283 | * niagara fb driver can communicate with kernel using a shared buffer.
|
---|
284 | */
|
---|
285 | //output_buffer.read_ptr = 0;
|
---|
286 | //output_buffer.write_ptr = 0;
|
---|
287 |
|
---|
288 | #if 0
|
---|
289 | sysinfo_set_item_val("niagara.outbuf.address", NULL,
|
---|
290 | KA2PA(&output_buffer));
|
---|
291 | sysinfo_set_item_val("niagara.outbuf.size", NULL,
|
---|
292 | PAGE_SIZE);
|
---|
293 | sysinfo_set_item_val("niagara.outbuf.datasize", NULL,
|
---|
294 | OUTPUT_BUFFER_SIZE);
|
---|
295 | static parea_t outbuf_parea;
|
---|
296 | outbuf_parea.pbase = (uintptr_t) (KA2PA(&output_buffer));
|
---|
297 | outbuf_parea.vbase = (uintptr_t) (&output_buffer);
|
---|
298 | outbuf_parea.frames = 1;
|
---|
299 | outbuf_parea.cacheable = false;
|
---|
300 | ddi_parea_register(&outbuf_parea);
|
---|
301 |
|
---|
302 | #endif
|
---|
303 |
|
---|
304 | outdev_t *niagara_dev = malloc(sizeof(outdev_t), FRAME_ATOMIC);
|
---|
305 | outdev_initialize("niagara_dev", niagara_dev, &niagara_ops);
|
---|
306 | stdout_wire(niagara_dev);
|
---|
307 | }
|
---|
308 |
|
---|
309 | /**
|
---|
310 | * A public function which initializes input from the Niagara console.
|
---|
311 | */
|
---|
312 | niagara_instance_t *niagarain_init(void)
|
---|
313 | {
|
---|
314 | niagara_init();
|
---|
315 |
|
---|
316 | if (instance) {
|
---|
317 | srln_instance_t *srln_instance = srln_init();
|
---|
318 | if (srln_instance) {
|
---|
319 | indev_t *sink = stdin_wire();
|
---|
320 | indev_t *srln = srln_wire(srln_instance, sink);
|
---|
321 |
|
---|
322 | // wire std. input to niagara
|
---|
323 | instance->srlnin = srln;
|
---|
324 | thread_ready(instance->thread);
|
---|
325 | }
|
---|
326 | }
|
---|
327 | return instance;
|
---|
328 | }
|
---|
329 |
|
---|
330 | /** @}
|
---|
331 | */
|
---|