source: mainline/kernel/arch/sparc64/src/drivers/niagara.c@ 86018c1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 86018c1 was 86018c1, checked in by Pavel Rimsky <pavel@…>, 15 years ago

Implemented uspace keyboard driver. Cleanup needed.

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