source: mainline/kernel/arch/sparc64/src/drivers/niagara.c@ 99de22b

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

Kernel MM implemented.

  • Property mode set to 100644
File size: 8.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#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
66static niagara_instance_t *instance = NULL;
67
68static void niagara_putchar(outdev_t *, const wchar_t, bool);
69
70/** character device operations */
71static 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/*
88#define OUTPUT_BUFFER_SIZE ((PAGE_SIZE) - 2 * 8)
89static volatile struct {
90 uint64_t read_ptr;
91 uint64_t write_ptr;
92 char data[OUTPUT_BUFFER_SIZE];
93}
94 __attribute__ ((packed))
95 __attribute__ ((aligned(PAGE_SIZE)))
96 output_buffer;
97*/
98
99#if 0
100/** Niagara character device */
101chardev_t niagara_io;
102
103/** defined in drivers/kbd.c */
104extern kbd_type_t kbd_type;
105
106/**
107 * The character read will be stored here until the (notified) uspace
108 * driver picks it up.
109 */
110static char read_char;
111
112
113#endif
114
115/** Writes a single character to the standard output. */
116static inline void do_putchar(const char c) {
117 /* repeat until the buffer is non-full */
118 while (__hypercall_fast1(CONS_PUTCHAR, c) == EWOULDBLOCK)
119 ;
120}
121
122/** Writes a single character to the standard output. */
123static void niagara_putchar(outdev_t *dev, const wchar_t ch, bool silent)
124{
125 do_putchar(ch);
126 if (ch == '\n')
127 do_putchar('\r');
128}
129
130#if 0
131/**
132 * Grabs the input for kernel.
133 */
134void niagara_grab(void)
135{
136 ipl_t ipl = interrupts_disable();
137 spinlock_lock(&niagara_irq.lock);
138 niagara_irq.notif_cfg.notify = false;
139 spinlock_unlock(&niagara_irq.lock);
140 interrupts_restore(ipl);
141}
142
143/**
144 * Releases the input so that userspace can use it.
145 */
146void niagara_release(void)
147{
148 ipl_t ipl = interrupts_disable();
149 spinlock_lock(&niagara_irq.lock);
150 if (niagara_irq.notif_cfg.answerbox)
151 niagara_irq.notif_cfg.notify = true;
152 spinlock_unlock(&niagara_irq.lock);
153 interrupts_restore(ipl);
154}
155
156/**
157 * Default suspend/resume operation for the input device.
158 */
159static void niagara_noop(chardev_t *d)
160{
161}
162
163/**
164 * Called when actively reading the character.
165 */
166static char niagara_read(chardev_t *d)
167{
168 uint64_t c;
169
170 if (__hypercall_fast_ret1(0, 0, 0, 0, 0, CONS_GETCHAR, &c) == EOK) {
171 return (char) c;
172 }
173
174 return '\0';
175}
176
177/**
178 * Returns the character last read. This function is called from the
179 * pseudocode - the character returned by this function is passed to
180 * the userspace keyboard driver.
181 */
182char niagara_getc(void) {
183 return read_char;
184}
185
186#endif
187
188/**
189 * Function regularly called by the keyboard polling thread. Asks the
190 * hypervisor whether there is any unread character. If so, it picks it up
191 * and sends it to the upper layers of HelenOS.
192 */
193static void niagara_poll(niagara_instance_t *instance)
194{
195 /*
196 while (output_buffer.read_ptr != output_buffer.write_ptr) {
197 do_putchar(output_buffer.data[output_buffer.read_ptr]);
198 output_buffer.read_ptr =
199 ((output_buffer.read_ptr) + 1) % OUTPUT_BUFFER_SIZE;
200 }
201 */
202
203 uint64_t c;
204
205 if (__hypercall_fast_ret1(0, 0, 0, 0, 0, CONS_GETCHAR, &c) == EOK) {
206 indev_push_character(instance->srlnin, c);
207 }
208
209}
210
211/**
212 * Polling thread function.
213 */
214static void kniagarapoll(void *instance) {
215 while (true) {
216 //MH
217 //if (!silent)
218 niagara_poll(instance);
219
220 thread_usleep(POLL_INTERVAL);
221 }
222}
223
224/**
225 * Initializes the input/output subsystem so that the Niagara standard
226 * input/output is used.
227 */
228static void niagara_init(void)
229{
230 if (instance)
231 return;
232
233 instance = malloc(sizeof(niagara_instance_t), FRAME_ATOMIC);
234
235 if (instance) {
236 instance->thread = thread_create(kniagarapoll, instance, TASK, 0,
237 "kniagarapoll", true);
238
239 if (!instance->thread) {
240 free(instance);
241 instance = NULL;
242 return;
243 }
244 }
245
246 instance->srlnin = NULL;
247
248 #if 0
249 kbd_type = KBD_SUN4V;
250
251 devno_t devno = device_assign_devno();
252 irq_initialize(&niagara_irq);
253 niagara_irq.devno = devno;
254 niagara_irq.inr = FICTIONAL_INR;
255 niagara_irq.claim = niagara_claim;
256 niagara_irq.handler = niagara_irq_handler;
257 irq_register(&niagara_irq);
258
259 sysinfo_set_item_val("kbd", NULL, true);
260 sysinfo_set_item_val("kbd.type", NULL, KBD_SUN4V);
261 sysinfo_set_item_val("kbd.devno", NULL, devno);
262 sysinfo_set_item_val("kbd.inr", NULL, FICTIONAL_INR);
263 sysinfo_set_item_val("fb.kind", NULL, 5);
264 #endif
265
266 /*
267 * Set sysinfos and pareas so that the userspace counterpart of the
268 * niagara fb driver can communicate with kernel using a shared buffer.
269 */
270 //output_buffer.read_ptr = 0;
271 //output_buffer.write_ptr = 0;
272
273 #if 0
274 sysinfo_set_item_val("niagara.outbuf.address", NULL,
275 KA2PA(&output_buffer));
276 sysinfo_set_item_val("niagara.outbuf.size", NULL,
277 PAGE_SIZE);
278 sysinfo_set_item_val("niagara.outbuf.datasize", NULL,
279 OUTPUT_BUFFER_SIZE);
280 static parea_t outbuf_parea;
281 outbuf_parea.pbase = (uintptr_t) (KA2PA(&output_buffer));
282 outbuf_parea.vbase = (uintptr_t) (&output_buffer);
283 outbuf_parea.frames = 1;
284 outbuf_parea.cacheable = false;
285 ddi_parea_register(&outbuf_parea);
286
287 #endif
288
289 outdev_t *niagara_dev = malloc(sizeof(outdev_t), FRAME_ATOMIC);
290 outdev_initialize("niagara_dev", niagara_dev, &niagara_ops);
291 stdout_wire(niagara_dev);
292}
293
294/**
295 * A public function which initializes input from the Niagara console.
296 */
297niagara_instance_t *niagarain_init(void)
298{
299 niagara_init();
300
301 if (instance) {
302 srln_instance_t *srln_instance = srln_init();
303 if (srln_instance) {
304 indev_t *sink = stdin_wire();
305 indev_t *srln = srln_wire(srln_instance, sink);
306
307 // wire std. input to niagara
308 instance->srlnin = srln;
309 thread_ready(instance->thread);
310 }
311 }
312 return instance;
313}
314
315/** @}
316 */
Note: See TracBrowser for help on using the repository browser.