| 1 | /*
|
|---|
| 2 | * Copyright (c) 2008 Pavel Rimsky
|
|---|
| 3 | * Copyright (c) 2017 Jiri Svoboda
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /** @file Sun4v console driver
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | #include <as.h>
|
|---|
| 34 | #include <async.h>
|
|---|
| 35 | #include <ddf/log.h>
|
|---|
| 36 | #include <ddi.h>
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <io/chardev_srv.h>
|
|---|
| 39 | #include <stdbool.h>
|
|---|
| 40 | #include <thread.h>
|
|---|
| 41 |
|
|---|
| 42 | #include "sun4v-con.h"
|
|---|
| 43 |
|
|---|
| 44 | static void sun4v_con_connection(ipc_callid_t, ipc_call_t *, void *);
|
|---|
| 45 |
|
|---|
| 46 | #define POLL_INTERVAL 10000
|
|---|
| 47 |
|
|---|
| 48 | /*
|
|---|
| 49 | * Kernel counterpart of the driver pushes characters (it has read) here.
|
|---|
| 50 | * Keep in sync with the definition from
|
|---|
| 51 | * kernel/arch/sparc64/src/drivers/niagara.c.
|
|---|
| 52 | */
|
|---|
| 53 | #define INPUT_BUFFER_SIZE ((PAGE_SIZE) - 2 * 8)
|
|---|
| 54 |
|
|---|
| 55 | typedef volatile struct {
|
|---|
| 56 | uint64_t write_ptr;
|
|---|
| 57 | uint64_t read_ptr;
|
|---|
| 58 | char data[INPUT_BUFFER_SIZE];
|
|---|
| 59 | } __attribute__((packed)) __attribute__((aligned(PAGE_SIZE))) *input_buffer_t;
|
|---|
| 60 |
|
|---|
| 61 | /* virtual address of the shared buffer */
|
|---|
| 62 | static input_buffer_t input_buffer;
|
|---|
| 63 |
|
|---|
| 64 | static int sun4v_con_read(chardev_srv_t *, void *, size_t, size_t *);
|
|---|
| 65 | static int sun4v_con_write(chardev_srv_t *, const void *, size_t, size_t *);
|
|---|
| 66 |
|
|---|
| 67 | static chardev_ops_t sun4v_con_chardev_ops = {
|
|---|
| 68 | .read = sun4v_con_read,
|
|---|
| 69 | .write = sun4v_con_write
|
|---|
| 70 | };
|
|---|
| 71 |
|
|---|
| 72 | static void sun4v_con_putchar(sun4v_con_t *con, uint8_t data)
|
|---|
| 73 | {
|
|---|
| 74 | (void) con;
|
|---|
| 75 | (void) data;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | /** Add sun4v console device. */
|
|---|
| 79 | int sun4v_con_add(sun4v_con_t *con, sun4v_con_res_t *res)
|
|---|
| 80 | {
|
|---|
| 81 | ddf_fun_t *fun = NULL;
|
|---|
| 82 | int rc;
|
|---|
| 83 |
|
|---|
| 84 | con->res = *res;
|
|---|
| 85 | input_buffer = (input_buffer_t) AS_AREA_ANY;
|
|---|
| 86 |
|
|---|
| 87 | fun = ddf_fun_create(con->dev, fun_exposed, "a");
|
|---|
| 88 | if (fun == NULL) {
|
|---|
| 89 | ddf_msg(LVL_ERROR, "Error creating function 'a'.");
|
|---|
| 90 | rc = ENOMEM;
|
|---|
| 91 | goto error;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | chardev_srvs_init(&con->cds);
|
|---|
| 95 | con->cds.ops = &sun4v_con_chardev_ops;
|
|---|
| 96 | con->cds.sarg = con;
|
|---|
| 97 |
|
|---|
| 98 | ddf_fun_set_conn_handler(fun, sun4v_con_connection);
|
|---|
| 99 |
|
|---|
| 100 | rc = physmem_map(res->base, 1, AS_AREA_READ | AS_AREA_WRITE,
|
|---|
| 101 | (void *) &input_buffer);
|
|---|
| 102 | if (rc != EOK) {
|
|---|
| 103 | ddf_msg(LVL_ERROR, "Error mapping memory: %d", rc);
|
|---|
| 104 | goto error;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | rc = ddf_fun_bind(fun);
|
|---|
| 108 | if (rc != EOK) {
|
|---|
| 109 | ddf_msg(LVL_ERROR, "Error binding function 'a'.");
|
|---|
| 110 | goto error;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | return EOK;
|
|---|
| 114 | error:
|
|---|
| 115 | /* XXX Clean up thread */
|
|---|
| 116 |
|
|---|
| 117 | if (input_buffer != (input_buffer_t) AS_AREA_ANY)
|
|---|
| 118 | physmem_unmap((void *) input_buffer);
|
|---|
| 119 |
|
|---|
| 120 | if (fun != NULL)
|
|---|
| 121 | ddf_fun_destroy(fun);
|
|---|
| 122 |
|
|---|
| 123 | return rc;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | /** Remove sun4v console device */
|
|---|
| 127 | int sun4v_con_remove(sun4v_con_t *con)
|
|---|
| 128 | {
|
|---|
| 129 | return ENOTSUP;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | /** Msim console device gone */
|
|---|
| 133 | int sun4v_con_gone(sun4v_con_t *con)
|
|---|
| 134 | {
|
|---|
| 135 | return ENOTSUP;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | /** Read from Sun4v console device */
|
|---|
| 139 | static int sun4v_con_read(chardev_srv_t *srv, void *buf, size_t size,
|
|---|
| 140 | size_t *nread)
|
|---|
| 141 | {
|
|---|
| 142 | size_t p;
|
|---|
| 143 | uint8_t *bp = (uint8_t *) buf;
|
|---|
| 144 | char c;
|
|---|
| 145 |
|
|---|
| 146 | while (input_buffer->read_ptr == input_buffer->write_ptr)
|
|---|
| 147 | fibril_usleep(POLL_INTERVAL);
|
|---|
| 148 |
|
|---|
| 149 | p = 0;
|
|---|
| 150 | while (p < size && input_buffer->read_ptr != input_buffer->write_ptr) {
|
|---|
| 151 | c = input_buffer->data[input_buffer->read_ptr];
|
|---|
| 152 | input_buffer->read_ptr =
|
|---|
| 153 | ((input_buffer->read_ptr) + 1) % INPUT_BUFFER_SIZE;
|
|---|
| 154 | bp[p++] = c;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | *nread = p;
|
|---|
| 158 | return EOK;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /** Write to Sun4v console device */
|
|---|
| 162 | static int sun4v_con_write(chardev_srv_t *srv, const void *data, size_t size,
|
|---|
| 163 | size_t *nwr)
|
|---|
| 164 | {
|
|---|
| 165 | sun4v_con_t *con = (sun4v_con_t *) srv->srvs->sarg;
|
|---|
| 166 | size_t i;
|
|---|
| 167 | uint8_t *dp = (uint8_t *) data;
|
|---|
| 168 |
|
|---|
| 169 | for (i = 0; i < size; i++)
|
|---|
| 170 | sun4v_con_putchar(con, dp[i]);
|
|---|
| 171 |
|
|---|
| 172 | *nwr = size;
|
|---|
| 173 | return EOK;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | /** Character device connection handler. */
|
|---|
| 177 | static void sun4v_con_connection(ipc_callid_t iid, ipc_call_t *icall,
|
|---|
| 178 | void *arg)
|
|---|
| 179 | {
|
|---|
| 180 | sun4v_con_t *con = (sun4v_con_t *) ddf_dev_data_get(
|
|---|
| 181 | ddf_fun_get_dev((ddf_fun_t *) arg));
|
|---|
| 182 |
|
|---|
| 183 | chardev_conn(iid, icall, &con->cds);
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | /** @}
|
|---|
| 187 | */
|
|---|