[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] | 58 | static niagara_instance_t *instance = NULL;
|
---|
| 59 |
|
---|
[b366a6f4] | 60 | static void niagara_putchar(outdev_t *, const wchar_t);
|
---|
[66e08d02] | 61 |
|
---|
[b366a6f4] | 62 | /** Character device operations */
|
---|
[66e08d02] | 63 | static outdev_operations_t niagara_ops = {
|
---|
| 64 | .write = niagara_putchar,
|
---|
| 65 | .redraw = NULL
|
---|
| 66 | };
|
---|
| 67 |
|
---|
[b366a6f4] | 68 | /**
|
---|
[66e08d02] | 69 | * The driver uses hypercalls to print characters to the console. Since the
|
---|
| 70 | * hypercall cannot be performed from the userspace, we do this:
|
---|
[b366a6f4] | 71 | *
|
---|
| 72 | * The kernel "little brother" driver (which will be present no matter what
|
---|
| 73 | * the DDI architecture is -- as we need the kernel part for the kconsole)
|
---|
[66e08d02] | 74 | * defines a shared buffer. Kernel walks through the buffer (in the same thread
|
---|
| 75 | * which is used for polling the keyboard) and prints any pending characters
|
---|
[b366a6f4] | 76 | * to the console (using hypercalls).
|
---|
| 77 | *
|
---|
| 78 | * The userspace fb server maps this shared buffer to its address space and
|
---|
| 79 | * output operation it does is performed using the mapped buffer. The shared
|
---|
| 80 | * buffer definition follows.
|
---|
[66e08d02] | 81 | */
|
---|
[b366a6f4] | 82 | #define OUTPUT_BUFFER_SIZE ((PAGE_SIZE) - 2 * 8)
|
---|
| 83 |
|
---|
[66e08d02] | 84 | static volatile struct {
|
---|
| 85 | uint64_t read_ptr;
|
---|
| 86 | uint64_t write_ptr;
|
---|
| 87 | char data[OUTPUT_BUFFER_SIZE];
|
---|
[b366a6f4] | 88 | } __attribute__ ((packed)) __attribute__ ((aligned(PAGE_SIZE))) output_buffer;
|
---|
| 89 |
|
---|
| 90 | static parea_t outbuf_parea;
|
---|
[66e08d02] | 91 |
|
---|
| 92 | /**
|
---|
[86018c1] | 93 | * Analogous to the output_buffer, see the previous definition.
|
---|
[66e08d02] | 94 | */
|
---|
[b366a6f4] | 95 | #define INPUT_BUFFER_SIZE ((PAGE_SIZE) - 2 * 8)
|
---|
| 96 |
|
---|
[86018c1] | 97 | static volatile struct {
|
---|
| 98 | uint64_t write_ptr;
|
---|
| 99 | uint64_t read_ptr;
|
---|
| 100 | char data[INPUT_BUFFER_SIZE];
|
---|
[b366a6f4] | 101 | } __attribute__ ((packed)) __attribute__ ((aligned(PAGE_SIZE))) input_buffer;
|
---|
[66e08d02] | 102 |
|
---|
[b366a6f4] | 103 | static parea_t inbuf_parea;
|
---|
[66e08d02] | 104 |
|
---|
[b366a6f4] | 105 | /** Write a single character to the standard output. */
|
---|
[66e08d02] | 106 | static inline void do_putchar(const char c) {
|
---|
[b366a6f4] | 107 | /* Repeat until the buffer is non-full */
|
---|
| 108 | while (__hypercall_fast1(CONS_PUTCHAR, c) == HV_EWOULDBLOCK);
|
---|
[66e08d02] | 109 | }
|
---|
| 110 |
|
---|
[b366a6f4] | 111 | /** Write a single character to the standard output. */
|
---|
| 112 | static void niagara_putchar(outdev_t *dev, const wchar_t ch)
|
---|
[66e08d02] | 113 | {
|
---|
[b366a6f4] | 114 | if ((!outbuf_parea.mapped) || (console_override)) {
|
---|
| 115 | do_putchar(ch);
|
---|
| 116 | if (ch == '\n')
|
---|
| 117 | do_putchar('\r');
|
---|
| 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] | 130 | static void niagara_poll(void)
|
---|
[66e08d02] | 131 | {
|
---|
[b366a6f4] | 132 | /*
|
---|
| 133 | * Print any pending characters from the
|
---|
| 134 | * shared buffer to the console.
|
---|
| 135 | */
|
---|
| 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 | }
|
---|
[b366a6f4] | 142 |
|
---|
| 143 | /*
|
---|
| 144 | * Read character from keyboard.
|
---|
| 145 | */
|
---|
| 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 | */
|
---|
[b366a6f4] | 170 | static void kniagarapoll(void *arg) {
|
---|
[69b68d1f] | 171 | while (true) {
|
---|
[b366a6f4] | 172 | niagara_poll();
|
---|
[69b68d1f] | 173 | thread_usleep(POLL_INTERVAL);
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
[66e08d02] | 176 |
|
---|
[b366a6f4] | 177 | /** Initialize the input/output subsystem
|
---|
| 178 | *
|
---|
[66e08d02] | 179 | */
|
---|
[69b68d1f] | 180 | static void niagara_init(void)
|
---|
[66e08d02] | 181 | {
|
---|
[69b68d1f] | 182 | if (instance)
|
---|
| 183 | return;
|
---|
| 184 |
|
---|
| 185 | instance = malloc(sizeof(niagara_instance_t), FRAME_ATOMIC);
|
---|
[b366a6f4] | 186 | instance->thread = thread_create(kniagarapoll, NULL, TASK, 0,
|
---|
| 187 | "kniagarapoll", true);
|
---|
[69b68d1f] | 188 |
|
---|
[b366a6f4] | 189 | if (!instance->thread) {
|
---|
| 190 | free(instance);
|
---|
| 191 | instance = NULL;
|
---|
| 192 | return;
|
---|
[69b68d1f] | 193 | }
|
---|
[b366a6f4] | 194 |
|
---|
[69b68d1f] | 195 | instance->srlnin = NULL;
|
---|
[b366a6f4] | 196 |
|
---|
[8e33e1d] | 197 | output_buffer.read_ptr = 0;
|
---|
| 198 | output_buffer.write_ptr = 0;
|
---|
| 199 | input_buffer.write_ptr = 0;
|
---|
| 200 | input_buffer.read_ptr = 0;
|
---|
[b366a6f4] | 201 |
|
---|
[eeb643d] | 202 | /*
|
---|
| 203 | * Set sysinfos and pareas so that the userspace counterpart of the
|
---|
[86018c1] | 204 | * niagara fb and kbd driver can communicate with kernel using shared
|
---|
| 205 | * buffers.
|
---|
[b366a6f4] | 206 | */
|
---|
| 207 |
|
---|
[8e33e1d] | 208 | sysinfo_set_item_val("fb.kind", NULL, 5);
|
---|
[b366a6f4] | 209 |
|
---|
[eeb643d] | 210 | sysinfo_set_item_val("niagara.outbuf.address", NULL,
|
---|
[b366a6f4] | 211 | KA2PA(&output_buffer));
|
---|
[eeb643d] | 212 | sysinfo_set_item_val("niagara.outbuf.size", NULL,
|
---|
[b366a6f4] | 213 | PAGE_SIZE);
|
---|
[eeb643d] | 214 | sysinfo_set_item_val("niagara.outbuf.datasize", NULL,
|
---|
[b366a6f4] | 215 | OUTPUT_BUFFER_SIZE);
|
---|
| 216 |
|
---|
[86018c1] | 217 | sysinfo_set_item_val("niagara.inbuf.address", NULL,
|
---|
[b366a6f4] | 218 | KA2PA(&input_buffer));
|
---|
[86018c1] | 219 | sysinfo_set_item_val("niagara.inbuf.size", NULL,
|
---|
[b366a6f4] | 220 | PAGE_SIZE);
|
---|
[86018c1] | 221 | sysinfo_set_item_val("niagara.inbuf.datasize", NULL,
|
---|
[b366a6f4] | 222 | INPUT_BUFFER_SIZE);
|
---|
| 223 |
|
---|
[66e08d02] | 224 | outbuf_parea.pbase = (uintptr_t) (KA2PA(&output_buffer));
|
---|
| 225 | outbuf_parea.frames = 1;
|
---|
[d7533c7] | 226 | outbuf_parea.unpriv = false;
|
---|
[b366a6f4] | 227 | outbuf_parea.mapped = false;
|
---|
[66e08d02] | 228 | ddi_parea_register(&outbuf_parea);
|
---|
[b366a6f4] | 229 |
|
---|
[86018c1] | 230 | inbuf_parea.pbase = (uintptr_t) (KA2PA(&input_buffer));
|
---|
| 231 | inbuf_parea.frames = 1;
|
---|
[d7533c7] | 232 | inbuf_parea.unpriv = false;
|
---|
[b366a6f4] | 233 | inbuf_parea.mapped = false;
|
---|
[86018c1] | 234 | ddi_parea_register(&inbuf_parea);
|
---|
[b366a6f4] | 235 |
|
---|
[66e08d02] | 236 | outdev_t *niagara_dev = malloc(sizeof(outdev_t), FRAME_ATOMIC);
|
---|
| 237 | outdev_initialize("niagara_dev", niagara_dev, &niagara_ops);
|
---|
| 238 | stdout_wire(niagara_dev);
|
---|
| 239 | }
|
---|
| 240 |
|
---|
[b366a6f4] | 241 | /** Initialize input from the Niagara console.
|
---|
| 242 | *
|
---|
[69b68d1f] | 243 | */
|
---|
| 244 | niagara_instance_t *niagarain_init(void)
|
---|
| 245 | {
|
---|
| 246 | niagara_init();
|
---|
[b366a6f4] | 247 |
|
---|
[69b68d1f] | 248 | if (instance) {
|
---|
| 249 | srln_instance_t *srln_instance = srln_init();
|
---|
| 250 | if (srln_instance) {
|
---|
| 251 | indev_t *sink = stdin_wire();
|
---|
| 252 | indev_t *srln = srln_wire(srln_instance, sink);
|
---|
[b366a6f4] | 253 |
|
---|
[69b68d1f] | 254 | instance->srlnin = srln;
|
---|
| 255 | thread_ready(instance->thread);
|
---|
| 256 | }
|
---|
| 257 | }
|
---|
[b366a6f4] | 258 |
|
---|
[69b68d1f] | 259 | return instance;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[66e08d02] | 262 | /** @}
|
---|
| 263 | */
|
---|