source: mainline/kernel/arch/sparc64/src/drivers/niagara.c@ 8e33e1d

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

Cleanup.

  • Property mode set to 100644
File size: 7.2 KB
Line 
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 */
58static niagara_instance_t *instance = NULL;
59
60static void niagara_putchar(outdev_t *, const wchar_t, bool);
61
62/** character device operations */
63static 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)
80static 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)
93static 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. */
104static inline void do_putchar(const char c) {
105 /* repeat until the buffer is non-full */
106 while (__hypercall_fast1(CONS_PUTCHAR, c) == EWOULDBLOCK)
107 ;
108}
109
110/** Writes a single character to the standard output. */
111static void niagara_putchar(outdev_t *dev, const wchar_t ch, bool silent)
112{
113 do_putchar(ch);
114 if (ch == '\n')
115 do_putchar('\r');
116}
117
118/**
119 * Function regularly called by the keyboard polling thread. Asks the
120 * hypervisor whether there is any unread character. If so, it picks it up
121 * and sends it to the upper layers of HelenOS.
122 *
123 * Apart from that, it also checks whether the userspace output driver has
124 * pushed any characters to the output buffer. If so, it prints them.
125 */
126static void niagara_poll(niagara_instance_t *instance)
127{
128 /* print any pending characters from the shared buffer to the console */
129 while (output_buffer.read_ptr != output_buffer.write_ptr) {
130 do_putchar(output_buffer.data[output_buffer.read_ptr]);
131 output_buffer.read_ptr =
132 ((output_buffer.read_ptr) + 1) % OUTPUT_BUFFER_SIZE;
133 }
134
135 uint64_t c;
136
137 /* read character from keyboard, send it to upper layers of HelenOS */
138 if (__hypercall_fast_ret1(0, 0, 0, 0, 0, CONS_GETCHAR, &c) == EOK) {
139 if (!silent) {
140 /* kconsole active, send the character to kernel */
141 indev_push_character(instance->srlnin, c);
142 } else {
143 /* kconsole inactive, send the character to uspace driver */
144 input_buffer.data[input_buffer.write_ptr] = (char) c;
145 input_buffer.write_ptr =
146 ((input_buffer.write_ptr) + 1) % INPUT_BUFFER_SIZE;
147 }
148 }
149}
150
151/**
152 * Polling thread function.
153 */
154static void kniagarapoll(void *instance) {
155 while (true) {
156 niagara_poll(instance);
157 thread_usleep(POLL_INTERVAL);
158 }
159}
160
161/**
162 * Initializes the input/output subsystem so that the Niagara standard
163 * input/output is used.
164 */
165static void niagara_init(void)
166{
167 if (instance)
168 return;
169
170 instance = malloc(sizeof(niagara_instance_t), FRAME_ATOMIC);
171
172 if (instance) {
173 instance->thread = thread_create(kniagarapoll, instance, TASK, 0,
174 "kniagarapoll", true);
175
176 if (!instance->thread) {
177 free(instance);
178 instance = NULL;
179 return;
180 }
181 }
182
183 instance->srlnin = NULL;
184
185 output_buffer.read_ptr = 0;
186 output_buffer.write_ptr = 0;
187 input_buffer.write_ptr = 0;
188 input_buffer.read_ptr = 0;
189
190 /*
191 * Set sysinfos and pareas so that the userspace counterpart of the
192 * niagara fb and kbd driver can communicate with kernel using shared
193 * buffers.
194 */
195
196 sysinfo_set_item_val("fb.kind", NULL, 5);
197
198 sysinfo_set_item_val("niagara.outbuf.address", NULL,
199 KA2PA(&output_buffer));
200 sysinfo_set_item_val("niagara.outbuf.size", NULL,
201 PAGE_SIZE);
202 sysinfo_set_item_val("niagara.outbuf.datasize", NULL,
203 OUTPUT_BUFFER_SIZE);
204
205 sysinfo_set_item_val("niagara.inbuf.address", NULL,
206 KA2PA(&input_buffer));
207 sysinfo_set_item_val("niagara.inbuf.size", NULL,
208 PAGE_SIZE);
209 sysinfo_set_item_val("niagara.inbuf.datasize", NULL,
210 INPUT_BUFFER_SIZE);
211
212 static parea_t outbuf_parea;
213 outbuf_parea.pbase = (uintptr_t) (KA2PA(&output_buffer));
214 outbuf_parea.frames = 1;
215 ddi_parea_register(&outbuf_parea);
216
217 static parea_t inbuf_parea;
218 inbuf_parea.pbase = (uintptr_t) (KA2PA(&input_buffer));
219 inbuf_parea.frames = 1;
220 ddi_parea_register(&inbuf_parea);
221
222 outdev_t *niagara_dev = malloc(sizeof(outdev_t), FRAME_ATOMIC);
223 outdev_initialize("niagara_dev", niagara_dev, &niagara_ops);
224 stdout_wire(niagara_dev);
225}
226
227/**
228 * A public function which initializes input from the Niagara console.
229 */
230niagara_instance_t *niagarain_init(void)
231{
232 niagara_init();
233
234 if (instance) {
235 srln_instance_t *srln_instance = srln_init();
236 if (srln_instance) {
237 indev_t *sink = stdin_wire();
238 indev_t *srln = srln_wire(srln_instance, sink);
239
240 // wire std. input to niagara
241 instance->srlnin = srln;
242 thread_ready(instance->thread);
243 }
244 }
245 return instance;
246}
247
248/** @}
249 */
Note: See TracBrowser for help on using the repository browser.