| 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 |
|
|---|
| 41 | #include "sun4v-con.h"
|
|---|
| 42 |
|
|---|
| 43 | static void sun4v_con_connection(ipc_callid_t, ipc_call_t *, void *);
|
|---|
| 44 |
|
|---|
| 45 | #define POLL_INTERVAL 10000
|
|---|
| 46 |
|
|---|
| 47 | static int sun4v_con_read(chardev_srv_t *, void *, size_t, size_t *);
|
|---|
| 48 | static int sun4v_con_write(chardev_srv_t *, const void *, size_t, size_t *);
|
|---|
| 49 |
|
|---|
| 50 | static chardev_ops_t sun4v_con_chardev_ops = {
|
|---|
| 51 | .read = sun4v_con_read,
|
|---|
| 52 | .write = sun4v_con_write
|
|---|
| 53 | };
|
|---|
| 54 |
|
|---|
| 55 | static void sun4v_con_putchar(sun4v_con_t *con, uint8_t data)
|
|---|
| 56 | {
|
|---|
| 57 | if (data == '\n')
|
|---|
| 58 | sun4v_con_putchar(con, '\r');
|
|---|
| 59 |
|
|---|
| 60 | while (con->output_buffer->write_ptr ==
|
|---|
| 61 | (con->output_buffer->read_ptr + OUTPUT_BUFFER_SIZE - 1)
|
|---|
| 62 | % OUTPUT_BUFFER_SIZE);
|
|---|
| 63 |
|
|---|
| 64 | con->output_buffer->data[con->output_buffer->write_ptr] = data;
|
|---|
| 65 | con->output_buffer->write_ptr =
|
|---|
| 66 | ((con->output_buffer->write_ptr) + 1) % OUTPUT_BUFFER_SIZE;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | /** Add sun4v console device. */
|
|---|
| 70 | int sun4v_con_add(sun4v_con_t *con, sun4v_con_res_t *res)
|
|---|
| 71 | {
|
|---|
| 72 | ddf_fun_t *fun = NULL;
|
|---|
| 73 | int rc;
|
|---|
| 74 |
|
|---|
| 75 | con->res = *res;
|
|---|
| 76 | con->input_buffer = (niagara_input_buffer_t *) AS_AREA_ANY;
|
|---|
| 77 |
|
|---|
| 78 | fun = ddf_fun_create(con->dev, fun_exposed, "a");
|
|---|
| 79 | if (fun == NULL) {
|
|---|
| 80 | ddf_msg(LVL_ERROR, "Error creating function 'a'.");
|
|---|
| 81 | rc = ENOMEM;
|
|---|
| 82 | goto error;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | chardev_srvs_init(&con->cds);
|
|---|
| 86 | con->cds.ops = &sun4v_con_chardev_ops;
|
|---|
| 87 | con->cds.sarg = con;
|
|---|
| 88 |
|
|---|
| 89 | ddf_fun_set_conn_handler(fun, sun4v_con_connection);
|
|---|
| 90 |
|
|---|
| 91 | rc = physmem_map(res->in_base, 1, AS_AREA_READ | AS_AREA_WRITE,
|
|---|
| 92 | (void *) &con->input_buffer);
|
|---|
| 93 | if (rc != EOK) {
|
|---|
| 94 | ddf_msg(LVL_ERROR, "Error mapping memory: %d", rc);
|
|---|
| 95 | goto error;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | con->output_buffer = (niagara_output_buffer_t *) AS_AREA_ANY;
|
|---|
| 99 |
|
|---|
| 100 | rc = physmem_map(res->out_base, 1, AS_AREA_READ | AS_AREA_WRITE,
|
|---|
| 101 | (void *) &con->output_buffer);
|
|---|
| 102 | if (rc != EOK) {
|
|---|
| 103 | ddf_msg(LVL_ERROR, "Error mapping memory: %d", rc);
|
|---|
| 104 | return rc;
|
|---|
| 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 | ddf_fun_add_to_category(fun, "console");
|
|---|
| 114 |
|
|---|
| 115 | return EOK;
|
|---|
| 116 | error:
|
|---|
| 117 | if (con->input_buffer != (niagara_input_buffer_t *) AS_AREA_ANY)
|
|---|
| 118 | physmem_unmap((void *) con->input_buffer);
|
|---|
| 119 |
|
|---|
| 120 | if (con->output_buffer != (niagara_output_buffer_t *) AS_AREA_ANY)
|
|---|
| 121 | physmem_unmap((void *) con->output_buffer);
|
|---|
| 122 |
|
|---|
| 123 | if (fun != NULL)
|
|---|
| 124 | ddf_fun_destroy(fun);
|
|---|
| 125 |
|
|---|
| 126 | return rc;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | /** Remove sun4v console device */
|
|---|
| 130 | int sun4v_con_remove(sun4v_con_t *con)
|
|---|
| 131 | {
|
|---|
| 132 | return ENOTSUP;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /** Msim console device gone */
|
|---|
| 136 | int sun4v_con_gone(sun4v_con_t *con)
|
|---|
| 137 | {
|
|---|
| 138 | return ENOTSUP;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | /** Read from Sun4v console device */
|
|---|
| 142 | static int sun4v_con_read(chardev_srv_t *srv, void *buf, size_t size,
|
|---|
| 143 | size_t *nread)
|
|---|
| 144 | {
|
|---|
| 145 | sun4v_con_t *con = (sun4v_con_t *) srv->srvs->sarg;
|
|---|
| 146 | size_t p;
|
|---|
| 147 | uint8_t *bp = (uint8_t *) buf;
|
|---|
| 148 | char c;
|
|---|
| 149 |
|
|---|
| 150 | while (con->input_buffer->read_ptr == con->input_buffer->write_ptr)
|
|---|
| 151 | async_usleep(POLL_INTERVAL);
|
|---|
| 152 |
|
|---|
| 153 | p = 0;
|
|---|
| 154 | while (p < size && con->input_buffer->read_ptr != con->input_buffer->write_ptr) {
|
|---|
| 155 | c = con->input_buffer->data[con->input_buffer->read_ptr];
|
|---|
| 156 | con->input_buffer->read_ptr =
|
|---|
| 157 | ((con->input_buffer->read_ptr) + 1) % INPUT_BUFFER_SIZE;
|
|---|
| 158 | bp[p++] = c;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | *nread = p;
|
|---|
| 162 | return EOK;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | /** Write to Sun4v console device */
|
|---|
| 166 | static int sun4v_con_write(chardev_srv_t *srv, const void *data, size_t size,
|
|---|
| 167 | size_t *nwr)
|
|---|
| 168 | {
|
|---|
| 169 | sun4v_con_t *con = (sun4v_con_t *) srv->srvs->sarg;
|
|---|
| 170 | size_t i;
|
|---|
| 171 | uint8_t *dp = (uint8_t *) data;
|
|---|
| 172 |
|
|---|
| 173 | for (i = 0; i < size; i++)
|
|---|
| 174 | sun4v_con_putchar(con, dp[i]);
|
|---|
| 175 |
|
|---|
| 176 | *nwr = size;
|
|---|
| 177 | return EOK;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | /** Character device connection handler. */
|
|---|
| 181 | static void sun4v_con_connection(ipc_callid_t iid, ipc_call_t *icall,
|
|---|
| 182 | void *arg)
|
|---|
| 183 | {
|
|---|
| 184 | sun4v_con_t *con = (sun4v_con_t *) ddf_dev_data_get(
|
|---|
| 185 | ddf_fun_get_dev((ddf_fun_t *) arg));
|
|---|
| 186 |
|
|---|
| 187 | chardev_conn(iid, icall, &con->cds);
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | /** @}
|
|---|
| 191 | */
|
|---|