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 | /* polling interval in miliseconds */
|
---|
55 | #define POLL_INTERVAL 10000
|
---|
56 |
|
---|
57 | /* device instance */
|
---|
58 | static niagara_instance_t *instance = NULL;
|
---|
59 |
|
---|
60 | static void niagara_putchar(outdev_t *, const wchar_t, bool);
|
---|
61 |
|
---|
62 | /** character device operations */
|
---|
63 | static outdev_operations_t niagara_ops = {
|
---|
64 | .write = niagara_putchar,
|
---|
65 | .redraw = NULL
|
---|
66 | };
|
---|
67 |
|
---|
68 | /*
|
---|
69 | * The driver uses hypercalls to print characters to the console. Since the
|
---|
70 | * hypercall cannot be performed from the userspace, we do this:
|
---|
71 | * The kernel "little brother" driver (which will be present no matter what the
|
---|
72 | * DDI architecture is - as we need the kernel part for the kconsole)
|
---|
73 | * defines a shared buffer. Kernel walks through the buffer (in the same thread
|
---|
74 | * which is used for polling the keyboard) and prints any pending characters
|
---|
75 | * to the console (using hypercalls). The userspace fb server maps this shared
|
---|
76 | * buffer to its address space and output operation it does is performed using
|
---|
77 | * the mapped buffer. The shared buffer definition follows.
|
---|
78 | */
|
---|
79 | #define OUTPUT_BUFFER_SIZE ((PAGE_SIZE) - 2 * 8)
|
---|
80 | static volatile struct {
|
---|
81 | uint64_t read_ptr;
|
---|
82 | uint64_t write_ptr;
|
---|
83 | char data[OUTPUT_BUFFER_SIZE];
|
---|
84 | }
|
---|
85 | __attribute__ ((packed))
|
---|
86 | __attribute__ ((aligned(PAGE_SIZE)))
|
---|
87 | output_buffer;
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Analogous to the output_buffer, see the previous definition.
|
---|
91 | */
|
---|
92 | #define INPUT_BUFFER_SIZE ((PAGE_SIZE) - 2 * 8)
|
---|
93 | static volatile struct {
|
---|
94 | uint64_t write_ptr;
|
---|
95 | uint64_t read_ptr;
|
---|
96 | char data[INPUT_BUFFER_SIZE];
|
---|
97 | }
|
---|
98 | __attribute__ ((packed))
|
---|
99 | __attribute__ ((aligned(PAGE_SIZE)))
|
---|
100 | input_buffer;
|
---|
101 |
|
---|
102 |
|
---|
103 | /** Writes a single character to the standard output. */
|
---|
104 | static inline void do_putchar(const char c) {
|
---|
105 | /* repeat until the buffer is non-full */
|
---|
106 | while (__hypercall_fast1(CONS_PUTCHAR, c) == HV_EWOULDBLOCK)
|
---|
107 | ;
|
---|
108 | }
|
---|
109 |
|
---|
110 | /** Writes a single character to the standard output. */
|
---|
111 | static void niagara_putchar(outdev_t *dev, const wchar_t ch, bool silent)
|
---|
112 | {
|
---|
113 | if (silent)
|
---|
114 | return;
|
---|
115 |
|
---|
116 | do_putchar(ch);
|
---|
117 | if (ch == '\n')
|
---|
118 | do_putchar('\r');
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Function regularly called by the keyboard polling thread. Asks the
|
---|
123 | * hypervisor whether there is any unread character. If so, it picks it up
|
---|
124 | * and sends it to the upper layers of HelenOS.
|
---|
125 | *
|
---|
126 | * Apart from that, it also checks whether the userspace output driver has
|
---|
127 | * pushed any characters to the output buffer. If so, it prints them.
|
---|
128 | */
|
---|
129 | static void niagara_poll(niagara_instance_t *instance)
|
---|
130 | {
|
---|
131 | /* print any pending characters from the shared buffer to the console */
|
---|
132 | while (output_buffer.read_ptr != output_buffer.write_ptr) {
|
---|
133 | do_putchar(output_buffer.data[output_buffer.read_ptr]);
|
---|
134 | output_buffer.read_ptr =
|
---|
135 | ((output_buffer.read_ptr) + 1) % OUTPUT_BUFFER_SIZE;
|
---|
136 | }
|
---|
137 |
|
---|
138 | uint64_t c;
|
---|
139 |
|
---|
140 | /* read character from keyboard, send it to upper layers of HelenOS */
|
---|
141 | if (__hypercall_fast_ret1(0, 0, 0, 0, 0, CONS_GETCHAR, &c) == HV_EOK) {
|
---|
142 | if (!silent) {
|
---|
143 | /* kconsole active, send the character to kernel */
|
---|
144 | indev_push_character(instance->srlnin, c);
|
---|
145 | } else {
|
---|
146 | /* kconsole inactive, send the character to uspace driver */
|
---|
147 | input_buffer.data[input_buffer.write_ptr] = (char) c;
|
---|
148 | input_buffer.write_ptr =
|
---|
149 | ((input_buffer.write_ptr) + 1) % INPUT_BUFFER_SIZE;
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Polling thread function.
|
---|
156 | */
|
---|
157 | static void kniagarapoll(void *instance) {
|
---|
158 | while (true) {
|
---|
159 | niagara_poll(instance);
|
---|
160 | thread_usleep(POLL_INTERVAL);
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Initializes the input/output subsystem so that the Niagara standard
|
---|
166 | * input/output is used.
|
---|
167 | */
|
---|
168 | static void niagara_init(void)
|
---|
169 | {
|
---|
170 | if (instance)
|
---|
171 | return;
|
---|
172 |
|
---|
173 | instance = malloc(sizeof(niagara_instance_t), FRAME_ATOMIC);
|
---|
174 |
|
---|
175 | if (instance) {
|
---|
176 | instance->thread = thread_create(kniagarapoll, instance, TASK, 0,
|
---|
177 | "kniagarapoll", true);
|
---|
178 |
|
---|
179 | if (!instance->thread) {
|
---|
180 | free(instance);
|
---|
181 | instance = NULL;
|
---|
182 | return;
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | instance->srlnin = NULL;
|
---|
187 |
|
---|
188 | output_buffer.read_ptr = 0;
|
---|
189 | output_buffer.write_ptr = 0;
|
---|
190 | input_buffer.write_ptr = 0;
|
---|
191 | input_buffer.read_ptr = 0;
|
---|
192 |
|
---|
193 | /*
|
---|
194 | * Set sysinfos and pareas so that the userspace counterpart of the
|
---|
195 | * niagara fb and kbd driver can communicate with kernel using shared
|
---|
196 | * buffers.
|
---|
197 | */
|
---|
198 |
|
---|
199 | sysinfo_set_item_val("fb.kind", NULL, 5);
|
---|
200 |
|
---|
201 | sysinfo_set_item_val("niagara.outbuf.address", NULL,
|
---|
202 | KA2PA(&output_buffer));
|
---|
203 | sysinfo_set_item_val("niagara.outbuf.size", NULL,
|
---|
204 | PAGE_SIZE);
|
---|
205 | sysinfo_set_item_val("niagara.outbuf.datasize", NULL,
|
---|
206 | OUTPUT_BUFFER_SIZE);
|
---|
207 |
|
---|
208 | sysinfo_set_item_val("niagara.inbuf.address", NULL,
|
---|
209 | KA2PA(&input_buffer));
|
---|
210 | sysinfo_set_item_val("niagara.inbuf.size", NULL,
|
---|
211 | PAGE_SIZE);
|
---|
212 | sysinfo_set_item_val("niagara.inbuf.datasize", NULL,
|
---|
213 | INPUT_BUFFER_SIZE);
|
---|
214 |
|
---|
215 | static parea_t outbuf_parea;
|
---|
216 | outbuf_parea.pbase = (uintptr_t) (KA2PA(&output_buffer));
|
---|
217 | outbuf_parea.frames = 1;
|
---|
218 | ddi_parea_register(&outbuf_parea);
|
---|
219 |
|
---|
220 | static parea_t inbuf_parea;
|
---|
221 | inbuf_parea.pbase = (uintptr_t) (KA2PA(&input_buffer));
|
---|
222 | inbuf_parea.frames = 1;
|
---|
223 | ddi_parea_register(&inbuf_parea);
|
---|
224 |
|
---|
225 | outdev_t *niagara_dev = malloc(sizeof(outdev_t), FRAME_ATOMIC);
|
---|
226 | outdev_initialize("niagara_dev", niagara_dev, &niagara_ops);
|
---|
227 | stdout_wire(niagara_dev);
|
---|
228 | }
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * A public function which initializes input from the Niagara console.
|
---|
232 | */
|
---|
233 | niagara_instance_t *niagarain_init(void)
|
---|
234 | {
|
---|
235 | niagara_init();
|
---|
236 |
|
---|
237 | if (instance) {
|
---|
238 | srln_instance_t *srln_instance = srln_init();
|
---|
239 | if (srln_instance) {
|
---|
240 | indev_t *sink = stdin_wire();
|
---|
241 | indev_t *srln = srln_wire(srln_instance, sink);
|
---|
242 |
|
---|
243 | // wire std. input to niagara
|
---|
244 | instance->srlnin = srln;
|
---|
245 | thread_ready(instance->thread);
|
---|
246 | }
|
---|
247 | }
|
---|
248 | return instance;
|
---|
249 | }
|
---|
250 |
|
---|
251 | /** @}
|
---|
252 | */
|
---|