source: mainline/uspace/drv/char/sun4v-con/sun4v-con.c@ f4cfd271

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f4cfd271 was f4cfd271, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Sun4v-con should get output buffer address via hw_res, too.

  • Property mode set to 100644
File size: 5.7 KB
Line 
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
43static void sun4v_con_connection(ipc_callid_t, ipc_call_t *, void *);
44
45#define POLL_INTERVAL 10000
46
47/*
48 * Kernel counterpart of the driver pushes characters (it has read) here.
49 * Keep in sync with the definition from
50 * kernel/arch/sparc64/src/drivers/niagara.c.
51 */
52#define INPUT_BUFFER_SIZE ((PAGE_SIZE) - 2 * 8)
53
54typedef volatile struct {
55 uint64_t write_ptr;
56 uint64_t read_ptr;
57 char data[INPUT_BUFFER_SIZE];
58} __attribute__((packed)) __attribute__((aligned(PAGE_SIZE))) *input_buffer_t;
59
60#define OUTPUT_FIFO_SIZE ((PAGE_SIZE) - 2 * sizeof(uint64_t))
61
62typedef volatile struct {
63 uint64_t read_ptr;
64 uint64_t write_ptr;
65 char data[OUTPUT_FIFO_SIZE];
66} __attribute__((packed)) output_fifo_t;
67
68/* virtual address of the shared buffer */
69static input_buffer_t input_buffer;
70static output_fifo_t *output_fifo;
71
72static int sun4v_con_read(chardev_srv_t *, void *, size_t, size_t *);
73static int sun4v_con_write(chardev_srv_t *, const void *, size_t, size_t *);
74
75static chardev_ops_t sun4v_con_chardev_ops = {
76 .read = sun4v_con_read,
77 .write = sun4v_con_write
78};
79
80static void sun4v_con_putchar(sun4v_con_t *con, uint8_t data)
81{
82 if (data == '\n')
83 sun4v_con_putchar(con, '\r');
84
85 while (output_fifo->write_ptr ==
86 (output_fifo->read_ptr + OUTPUT_FIFO_SIZE - 1)
87 % OUTPUT_FIFO_SIZE);
88
89 output_fifo->data[output_fifo->write_ptr] = data;
90 output_fifo->write_ptr =
91 ((output_fifo->write_ptr) + 1) % OUTPUT_FIFO_SIZE;
92}
93
94/** Add sun4v console device. */
95int sun4v_con_add(sun4v_con_t *con, sun4v_con_res_t *res)
96{
97 ddf_fun_t *fun = NULL;
98 int rc;
99
100 con->res = *res;
101 input_buffer = (input_buffer_t) AS_AREA_ANY;
102
103 fun = ddf_fun_create(con->dev, fun_exposed, "a");
104 if (fun == NULL) {
105 ddf_msg(LVL_ERROR, "Error creating function 'a'.");
106 rc = ENOMEM;
107 goto error;
108 }
109
110 chardev_srvs_init(&con->cds);
111 con->cds.ops = &sun4v_con_chardev_ops;
112 con->cds.sarg = con;
113
114 ddf_fun_set_conn_handler(fun, sun4v_con_connection);
115
116 rc = physmem_map(res->in_base, 1, AS_AREA_READ | AS_AREA_WRITE,
117 (void *) &input_buffer);
118 if (rc != EOK) {
119 ddf_msg(LVL_ERROR, "Error mapping memory: %d", rc);
120 goto error;
121 }
122
123 output_fifo = (output_fifo_t *) AS_AREA_ANY;
124
125 rc = physmem_map(res->out_base, 1, AS_AREA_READ | AS_AREA_WRITE,
126 (void *) &output_fifo);
127 if (rc != EOK) {
128 ddf_msg(LVL_ERROR, "Error mapping memory: %d", rc);
129 return rc;
130 }
131
132 rc = ddf_fun_bind(fun);
133 if (rc != EOK) {
134 ddf_msg(LVL_ERROR, "Error binding function 'a'.");
135 goto error;
136 }
137
138 ddf_fun_add_to_category(fun, "console");
139
140 return EOK;
141error:
142 if (input_buffer != (input_buffer_t) AS_AREA_ANY)
143 physmem_unmap((void *) input_buffer);
144
145 if (output_fifo != (output_fifo_t *) AS_AREA_ANY)
146 physmem_unmap((void *) output_fifo);
147
148 if (fun != NULL)
149 ddf_fun_destroy(fun);
150
151 return rc;
152}
153
154/** Remove sun4v console device */
155int sun4v_con_remove(sun4v_con_t *con)
156{
157 return ENOTSUP;
158}
159
160/** Msim console device gone */
161int sun4v_con_gone(sun4v_con_t *con)
162{
163 return ENOTSUP;
164}
165
166/** Read from Sun4v console device */
167static int sun4v_con_read(chardev_srv_t *srv, void *buf, size_t size,
168 size_t *nread)
169{
170 size_t p;
171 uint8_t *bp = (uint8_t *) buf;
172 char c;
173
174 while (input_buffer->read_ptr == input_buffer->write_ptr)
175 fibril_usleep(POLL_INTERVAL);
176
177 p = 0;
178 while (p < size && input_buffer->read_ptr != input_buffer->write_ptr) {
179 c = input_buffer->data[input_buffer->read_ptr];
180 input_buffer->read_ptr =
181 ((input_buffer->read_ptr) + 1) % INPUT_BUFFER_SIZE;
182 bp[p++] = c;
183 }
184
185 *nread = p;
186 return EOK;
187}
188
189/** Write to Sun4v console device */
190static int sun4v_con_write(chardev_srv_t *srv, const void *data, size_t size,
191 size_t *nwr)
192{
193 sun4v_con_t *con = (sun4v_con_t *) srv->srvs->sarg;
194 size_t i;
195 uint8_t *dp = (uint8_t *) data;
196
197 for (i = 0; i < size; i++)
198 sun4v_con_putchar(con, dp[i]);
199
200 *nwr = size;
201 return EOK;
202}
203
204/** Character device connection handler. */
205static void sun4v_con_connection(ipc_callid_t iid, ipc_call_t *icall,
206 void *arg)
207{
208 sun4v_con_t *con = (sun4v_con_t *) ddf_dev_data_get(
209 ddf_fun_get_dev((ddf_fun_t *) arg));
210
211 chardev_conn(iid, icall, &con->cds);
212}
213
214/** @}
215 */
Note: See TracBrowser for help on using the repository browser.