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