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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d70ebffe was a293fef, checked in by Jiri Svoboda <jiri@…>, 12 years ago

Fix Niagara FB sysinfo.

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