| 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 <arch/asm.h>
|
|---|
| 42 | #include <arch.h>
|
|---|
| 43 | #include <mm/slab.h>
|
|---|
| 44 | #include <arch/drivers/niagara_buf.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 */
|
|---|
| 58 | static niagara_instance_t *instance = NULL;
|
|---|
| 59 |
|
|---|
| 60 | static void niagara_putchar(outdev_t *, const wchar_t);
|
|---|
| 61 |
|
|---|
| 62 | /** Character device operations */
|
|---|
| 63 | static outdev_operations_t niagara_ops = {
|
|---|
| 64 | .write = niagara_putchar,
|
|---|
| 65 | .redraw = NULL,
|
|---|
| 66 | .scroll_up = NULL,
|
|---|
| 67 | .scroll_down = NULL
|
|---|
| 68 | };
|
|---|
| 69 |
|
|---|
| 70 | /**
|
|---|
| 71 | * The driver uses hypercalls to print characters to the console. Since the
|
|---|
| 72 | * hypercall cannot be performed from the userspace, we do this:
|
|---|
| 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)
|
|---|
| 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
|
|---|
| 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.
|
|---|
| 83 | */
|
|---|
| 84 | static volatile niagara_output_buffer_t __attribute__ ((aligned(PAGE_SIZE)))
|
|---|
| 85 | output_buffer;
|
|---|
| 86 |
|
|---|
| 87 | static parea_t outbuf_parea;
|
|---|
| 88 |
|
|---|
| 89 | /**
|
|---|
| 90 | * Analogous to the output_buffer, see the previous definition.
|
|---|
| 91 | */
|
|---|
| 92 | static volatile niagara_input_buffer_t __attribute__ ((aligned(PAGE_SIZE)))
|
|---|
| 93 | input_buffer;
|
|---|
| 94 |
|
|---|
| 95 | static parea_t inbuf_parea;
|
|---|
| 96 |
|
|---|
| 97 | /** Write a single character to the standard output. */
|
|---|
| 98 | static inline void do_putchar(const char c) {
|
|---|
| 99 | /* Repeat until the buffer is non-full */
|
|---|
| 100 | while (__hypercall_fast1(CONS_PUTCHAR, c) == HV_EWOULDBLOCK);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /** Write a single character to the standard output. */
|
|---|
| 104 | static void niagara_putchar(outdev_t *dev, const wchar_t ch)
|
|---|
| 105 | {
|
|---|
| 106 | if ((!outbuf_parea.mapped) || (console_override)) {
|
|---|
| 107 | do_putchar(ch);
|
|---|
| 108 | if (ch == '\n')
|
|---|
| 109 | do_putchar('\r');
|
|---|
| 110 | }
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /** Poll keyboard and print pending characters.
|
|---|
| 114 | *
|
|---|
| 115 | * Ask the hypervisor whether there is any unread character. If so,
|
|---|
| 116 | * pick it up and send it to the indev layer.
|
|---|
| 117 | *
|
|---|
| 118 | * Check whether the userspace output driver has pushed any
|
|---|
| 119 | * characters to the output buffer and eventually print them.
|
|---|
| 120 | *
|
|---|
| 121 | */
|
|---|
| 122 | static void niagara_poll(void)
|
|---|
| 123 | {
|
|---|
| 124 | /*
|
|---|
| 125 | * Print any pending characters from the
|
|---|
| 126 | * shared buffer to the console.
|
|---|
| 127 | */
|
|---|
| 128 |
|
|---|
| 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 | /*
|
|---|
| 136 | * Read character from keyboard.
|
|---|
| 137 | */
|
|---|
| 138 |
|
|---|
| 139 | uint64_t c;
|
|---|
| 140 | if (__hypercall_fast_ret1(0, 0, 0, 0, 0, CONS_GETCHAR, &c) == HV_EOK) {
|
|---|
| 141 | if ((!inbuf_parea.mapped) || (console_override)) {
|
|---|
| 142 | /*
|
|---|
| 143 | * Kernel console is active, send
|
|---|
| 144 | * the character to kernel.
|
|---|
| 145 | */
|
|---|
| 146 | indev_push_character(instance->srlnin, c);
|
|---|
| 147 | } else {
|
|---|
| 148 | /*
|
|---|
| 149 | * Kernel console is inactive, send
|
|---|
| 150 | * the character to uspace driver.
|
|---|
| 151 | */
|
|---|
| 152 | input_buffer.data[input_buffer.write_ptr] = (char) c;
|
|---|
| 153 | input_buffer.write_ptr =
|
|---|
| 154 | ((input_buffer.write_ptr) + 1) % INPUT_BUFFER_SIZE;
|
|---|
| 155 | }
|
|---|
| 156 | }
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | /** Polling thread function.
|
|---|
| 160 | *
|
|---|
| 161 | */
|
|---|
| 162 | static void kniagarapoll(void *arg) {
|
|---|
| 163 | while (true) {
|
|---|
| 164 | niagara_poll();
|
|---|
| 165 | thread_usleep(POLL_INTERVAL);
|
|---|
| 166 | }
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | /** Initialize the input/output subsystem
|
|---|
| 170 | *
|
|---|
| 171 | */
|
|---|
| 172 | static void niagara_init(void)
|
|---|
| 173 | {
|
|---|
| 174 | if (instance)
|
|---|
| 175 | return;
|
|---|
| 176 |
|
|---|
| 177 | instance = malloc(sizeof(niagara_instance_t), FRAME_ATOMIC);
|
|---|
| 178 | instance->thread = thread_create(kniagarapoll, NULL, TASK,
|
|---|
| 179 | THREAD_FLAG_UNCOUNTED, "kniagarapoll");
|
|---|
| 180 |
|
|---|
| 181 | if (!instance->thread) {
|
|---|
| 182 | free(instance);
|
|---|
| 183 | instance = NULL;
|
|---|
| 184 | return;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | instance->srlnin = NULL;
|
|---|
| 188 |
|
|---|
| 189 | output_buffer.read_ptr = 0;
|
|---|
| 190 | output_buffer.write_ptr = 0;
|
|---|
| 191 | input_buffer.write_ptr = 0;
|
|---|
| 192 | input_buffer.read_ptr = 0;
|
|---|
| 193 |
|
|---|
| 194 | /*
|
|---|
| 195 | * Set sysinfos and pareas so that the userspace counterpart of the
|
|---|
| 196 | * niagara fb and kbd driver can communicate with kernel using shared
|
|---|
| 197 | * buffers.
|
|---|
| 198 | */
|
|---|
| 199 |
|
|---|
| 200 | sysinfo_set_item_val("fb", NULL, true);
|
|---|
| 201 | sysinfo_set_item_val("fb.kind", NULL, 5);
|
|---|
| 202 |
|
|---|
| 203 | sysinfo_set_item_val("niagara.outbuf.address", NULL,
|
|---|
| 204 | KA2PA(&output_buffer));
|
|---|
| 205 | sysinfo_set_item_val("niagara.outbuf.size", NULL,
|
|---|
| 206 | PAGE_SIZE);
|
|---|
| 207 | sysinfo_set_item_val("niagara.outbuf.datasize", NULL,
|
|---|
| 208 | OUTPUT_BUFFER_SIZE);
|
|---|
| 209 |
|
|---|
| 210 | sysinfo_set_item_val("niagara.inbuf.address", NULL,
|
|---|
| 211 | KA2PA(&input_buffer));
|
|---|
| 212 | sysinfo_set_item_val("niagara.inbuf.size", NULL,
|
|---|
| 213 | PAGE_SIZE);
|
|---|
| 214 | sysinfo_set_item_val("niagara.inbuf.datasize", NULL,
|
|---|
| 215 | INPUT_BUFFER_SIZE);
|
|---|
| 216 |
|
|---|
| 217 | outbuf_parea.pbase = (uintptr_t) (KA2PA(&output_buffer));
|
|---|
| 218 | outbuf_parea.frames = 1;
|
|---|
| 219 | outbuf_parea.unpriv = false;
|
|---|
| 220 | outbuf_parea.mapped = false;
|
|---|
| 221 | ddi_parea_register(&outbuf_parea);
|
|---|
| 222 |
|
|---|
| 223 | inbuf_parea.pbase = (uintptr_t) (KA2PA(&input_buffer));
|
|---|
| 224 | inbuf_parea.frames = 1;
|
|---|
| 225 | inbuf_parea.unpriv = false;
|
|---|
| 226 | inbuf_parea.mapped = false;
|
|---|
| 227 | ddi_parea_register(&inbuf_parea);
|
|---|
| 228 |
|
|---|
| 229 | outdev_t *niagara_dev = malloc(sizeof(outdev_t), FRAME_ATOMIC);
|
|---|
| 230 | outdev_initialize("niagara_dev", niagara_dev, &niagara_ops);
|
|---|
| 231 | stdout_wire(niagara_dev);
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | /** Initialize input from the Niagara console.
|
|---|
| 235 | *
|
|---|
| 236 | */
|
|---|
| 237 | niagara_instance_t *niagarain_init(void)
|
|---|
| 238 | {
|
|---|
| 239 | niagara_init();
|
|---|
| 240 |
|
|---|
| 241 | if (instance) {
|
|---|
| 242 | srln_instance_t *srln_instance = srln_init();
|
|---|
| 243 | if (srln_instance) {
|
|---|
| 244 | indev_t *sink = stdin_wire();
|
|---|
| 245 | indev_t *srln = srln_wire(srln_instance, sink);
|
|---|
| 246 |
|
|---|
| 247 | instance->srlnin = srln;
|
|---|
| 248 | thread_ready(instance->thread);
|
|---|
| 249 | }
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | return instance;
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | /** @}
|
|---|
| 256 | */
|
|---|