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