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

Last change on this file was e9bc927, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 months ago

Update forgotten serial implementations

  • Property mode set to 100644
File size: 7.2 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
[c5429fe]29/** @addtogroup kernel_sparc64
[66e08d02]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 <arch/asm.h>
[69b68d1f]42#include <arch.h>
[aafed15]43#include <stdlib.h>
[d9ec808b]44#include <arch/drivers/niagara_buf.h>
[66e08d02]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 <proc/thread.h>
50#include <console/console.h>
51#include <genarch/srln/srln.h>
52
[b366a6f4]53/* Polling interval in miliseconds */
[69b68d1f]54#define POLL_INTERVAL 10000
[66e08d02]55
[b366a6f4]56/* Device instance */
[69b68d1f]57static niagara_instance_t *instance = NULL;
58
[e9bc927]59static void niagara_write(outdev_t *, const char *, size_t);
[66e08d02]60
[b366a6f4]61/** Character device operations */
[66e08d02]62static outdev_operations_t niagara_ops = {
[e9bc927]63 .write = niagara_write,
[7ddc2c7]64 .redraw = NULL,
65 .scroll_up = NULL,
66 .scroll_down = NULL
[66e08d02]67};
68
[b366a6f4]69/**
[66e08d02]70 * The driver uses hypercalls to print characters to the console. Since the
71 * hypercall cannot be performed from the userspace, we do this:
[b366a6f4]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)
[66e08d02]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
[b366a6f4]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.
[66e08d02]82 */
[1433ecda]83static volatile niagara_output_buffer_t __attribute__((aligned(PAGE_SIZE)))
[d9ec808b]84 output_buffer;
[b366a6f4]85
86static parea_t outbuf_parea;
[66e08d02]87
88/**
[86018c1]89 * Analogous to the output_buffer, see the previous definition.
[66e08d02]90 */
[1433ecda]91static volatile niagara_input_buffer_t __attribute__((aligned(PAGE_SIZE)))
[d9ec808b]92 input_buffer;
[66e08d02]93
[b366a6f4]94static parea_t inbuf_parea;
[66e08d02]95
[b366a6f4]96/** Write a single character to the standard output. */
[e9bc927]97static inline void do_putchar(uint8_t c)
[1433ecda]98{
[b366a6f4]99 /* Repeat until the buffer is non-full */
[1433ecda]100 while (__hypercall_fast1(CONS_PUTCHAR, c) == HV_EWOULDBLOCK)
101 ;
[66e08d02]102}
103
[e9bc927]104static void niagara_write(outdev_t *dev, const char *s, size_t n)
[66e08d02]105{
[e9bc927]106 /* If the userspace owns the console, do not output anything. */
107 if (outbuf_parea.mapped && !console_override)
108 return;
109
110 const char *top = s + n;
111 assert(top >= s);
112
113 for (; s < top; s++) {
114 if (*s == '\n')
115 do_putchar('\r');
116
117 do_putchar((uint8_t) *s);
[b366a6f4]118 }
[66e08d02]119}
120
[b366a6f4]121/** Poll keyboard and print pending characters.
122 *
123 * Ask the hypervisor whether there is any unread character. If so,
124 * pick it up and send it to the indev layer.
125 *
126 * Check whether the userspace output driver has pushed any
127 * characters to the output buffer and eventually print them.
[86018c1]128 *
[66e08d02]129 */
[b366a6f4]130static void niagara_poll(void)
[66e08d02]131{
[b366a6f4]132 /*
133 * Print any pending characters from the
134 * shared buffer to the console.
135 */
[a35b458]136
[66e08d02]137 while (output_buffer.read_ptr != output_buffer.write_ptr) {
138 do_putchar(output_buffer.data[output_buffer.read_ptr]);
139 output_buffer.read_ptr =
[b366a6f4]140 ((output_buffer.read_ptr) + 1) % OUTPUT_BUFFER_SIZE;
[66e08d02]141 }
[a35b458]142
[b366a6f4]143 /*
144 * Read character from keyboard.
145 */
[a35b458]146
[66e08d02]147 uint64_t c;
[7da160b]148 if (__hypercall_fast_ret1(0, 0, 0, 0, 0, CONS_GETCHAR, &c) == HV_EOK) {
[b366a6f4]149 if ((!inbuf_parea.mapped) || (console_override)) {
150 /*
151 * Kernel console is active, send
152 * the character to kernel.
153 */
[86018c1]154 indev_push_character(instance->srlnin, c);
155 } else {
[b366a6f4]156 /*
157 * Kernel console is inactive, send
158 * the character to uspace driver.
159 */
[86018c1]160 input_buffer.data[input_buffer.write_ptr] = (char) c;
161 input_buffer.write_ptr =
[b366a6f4]162 ((input_buffer.write_ptr) + 1) % INPUT_BUFFER_SIZE;
[86018c1]163 }
[66e08d02]164 }
165}
166
[b366a6f4]167/** Polling thread function.
168 *
[69b68d1f]169 */
[1433ecda]170static void kniagarapoll(void *arg)
171{
[69b68d1f]172 while (true) {
[b366a6f4]173 niagara_poll();
[69b68d1f]174 thread_usleep(POLL_INTERVAL);
175 }
176}
[66e08d02]177
[b366a6f4]178/** Initialize the input/output subsystem
179 *
[66e08d02]180 */
[69b68d1f]181static void niagara_init(void)
[66e08d02]182{
[69b68d1f]183 if (instance)
184 return;
[a35b458]185
[11b285d]186 instance = malloc(sizeof(niagara_instance_t));
[6eef3c4]187 instance->thread = thread_create(kniagarapoll, NULL, TASK,
188 THREAD_FLAG_UNCOUNTED, "kniagarapoll");
[a35b458]189
[b366a6f4]190 if (!instance->thread) {
191 free(instance);
192 instance = NULL;
193 return;
[69b68d1f]194 }
[a35b458]195
[69b68d1f]196 instance->srlnin = NULL;
[a35b458]197
[8e33e1d]198 output_buffer.read_ptr = 0;
199 output_buffer.write_ptr = 0;
200 input_buffer.write_ptr = 0;
201 input_buffer.read_ptr = 0;
[a35b458]202
[eeb643d]203 /*
204 * Set sysinfos and pareas so that the userspace counterpart of the
[86018c1]205 * niagara fb and kbd driver can communicate with kernel using shared
206 * buffers.
[b366a6f4]207 */
[a35b458]208
[a293fef]209 sysinfo_set_item_val("fb", NULL, true);
[8e33e1d]210 sysinfo_set_item_val("fb.kind", NULL, 5);
[a35b458]211
[eeb643d]212 sysinfo_set_item_val("niagara.outbuf.address", NULL,
[b366a6f4]213 KA2PA(&output_buffer));
[eeb643d]214 sysinfo_set_item_val("niagara.outbuf.size", NULL,
[b366a6f4]215 PAGE_SIZE);
[eeb643d]216 sysinfo_set_item_val("niagara.outbuf.datasize", NULL,
[b366a6f4]217 OUTPUT_BUFFER_SIZE);
[a35b458]218
[86018c1]219 sysinfo_set_item_val("niagara.inbuf.address", NULL,
[b366a6f4]220 KA2PA(&input_buffer));
[86018c1]221 sysinfo_set_item_val("niagara.inbuf.size", NULL,
[b366a6f4]222 PAGE_SIZE);
[86018c1]223 sysinfo_set_item_val("niagara.inbuf.datasize", NULL,
[1433ecda]224 INPUT_BUFFER_SIZE);
[a35b458]225
[6f7071b]226 ddi_parea_init(&outbuf_parea);
[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);
[a35b458]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);
[a35b458]238
[11b285d]239 outdev_t *niagara_dev = malloc(sizeof(outdev_t));
[66e08d02]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();
[a35b458]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);
[a35b458]256
[69b68d1f]257 instance->srlnin = srln;
[0f4f1b2]258 thread_start(instance->thread);
[69b68d1f]259 }
260 }
[a35b458]261
[69b68d1f]262 return instance;
263}
264
[66e08d02]265/** @}
266 */
Note: See TracBrowser for help on using the repository browser.