forked from AsahiLinux/m1n1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiodev.c
319 lines (253 loc) · 7.3 KB
/
iodev.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* SPDX-License-Identifier: MIT */
// #define DEBUG_IODEV
#include "iodev.h"
#include "memory.h"
#include "string.h"
#ifdef DEBUG_IODEV
#define dprintf printf
#else
#define dprintf(...) \
do { \
} while (0)
#endif
#define CONSOLE_BUFFER_SIZE 8192
extern struct iodev iodev_uart;
extern struct iodev iodev_fb;
extern struct iodev iodev_usb_vuart;
struct iodev *iodevs[IODEV_MAX] = {
[IODEV_UART] = &iodev_uart,
[IODEV_FB] = &iodev_fb,
[IODEV_USB_VUART] = &iodev_usb_vuart,
};
char con_buf[CONSOLE_BUFFER_SIZE];
size_t con_wp;
size_t con_rp[IODEV_MAX];
void iodev_register_device(iodev_id_t id, struct iodev *dev)
{
if (id >= IODEV_MAX)
return;
iodevs[id] = dev;
}
struct iodev *iodev_unregister_device(iodev_id_t id)
{
if (id < IODEV_USB0 || id >= IODEV_MAX)
return NULL;
struct iodev *dev = iodevs[id];
iodevs[id] = NULL;
return dev;
}
ssize_t iodev_can_read(iodev_id_t id)
{
if (!iodevs[id] || !iodevs[id]->ops->can_read)
return 0;
if (mmu_active())
spin_lock(&iodevs[id]->lock);
ssize_t ret = iodevs[id]->ops->can_read(iodevs[id]->opaque);
if (mmu_active())
spin_unlock(&iodevs[id]->lock);
return ret;
}
bool iodev_can_write(iodev_id_t id)
{
if (!iodevs[id] || !iodevs[id]->ops->can_write)
return false;
if (mmu_active())
spin_lock(&iodevs[id]->lock);
bool ret = iodevs[id]->ops->can_write(iodevs[id]->opaque);
if (mmu_active())
spin_unlock(&iodevs[id]->lock);
return ret;
}
ssize_t iodev_read(iodev_id_t id, void *buf, size_t length)
{
if (!iodevs[id] || !iodevs[id]->ops->read)
return -1;
if (mmu_active())
spin_lock(&iodevs[id]->lock);
ssize_t ret = iodevs[id]->ops->read(iodevs[id]->opaque, buf, length);
if (mmu_active())
spin_unlock(&iodevs[id]->lock);
return ret;
}
ssize_t iodev_write(iodev_id_t id, const void *buf, size_t length)
{
if (!iodevs[id] || !iodevs[id]->ops->write)
return -1;
if (mmu_active())
spin_lock(&iodevs[id]->lock);
ssize_t ret = iodevs[id]->ops->write(iodevs[id]->opaque, buf, length);
if (mmu_active())
spin_unlock(&iodevs[id]->lock);
return ret;
}
ssize_t iodev_queue(iodev_id_t id, const void *buf, size_t length)
{
if (!iodevs[id] || !iodevs[id]->ops->queue)
return iodev_write(id, buf, length);
if (mmu_active())
spin_lock(&iodevs[id]->lock);
ssize_t ret = iodevs[id]->ops->queue(iodevs[id]->opaque, buf, length);
if (mmu_active())
spin_unlock(&iodevs[id]->lock);
return ret;
}
void iodev_flush(iodev_id_t id)
{
if (!iodevs[id] || !iodevs[id]->ops->flush)
return;
if (mmu_active())
spin_lock(&iodevs[id]->lock);
iodevs[id]->ops->flush(iodevs[id]->opaque);
if (mmu_active())
spin_unlock(&iodevs[id]->lock);
}
void iodev_lock(iodev_id_t id)
{
if (!iodevs[id])
return;
if (mmu_active())
spin_lock(&iodevs[id]->lock);
}
void iodev_unlock(iodev_id_t id)
{
if (!iodevs[id])
return;
if (mmu_active())
spin_unlock(&iodevs[id]->lock);
}
int in_iodev = 0;
static DECLARE_SPINLOCK(console_lock);
void iodev_console_write(const void *buf, size_t length)
{
bool do_lock = mmu_active();
if (!do_lock && !is_primary_core()) {
if (length && iodevs[IODEV_UART]->usage & USAGE_CONSOLE) {
iodevs[IODEV_UART]->ops->write(iodevs[IODEV_UART]->opaque, "*", 1);
iodevs[IODEV_UART]->ops->write(iodevs[IODEV_UART]->opaque, buf, length);
}
return;
}
if (do_lock)
spin_lock(&console_lock);
if (in_iodev) {
if (length && iodevs[IODEV_UART]->usage & USAGE_CONSOLE) {
iodevs[IODEV_UART]->ops->write(iodevs[IODEV_UART]->opaque, "+", 1);
iodevs[IODEV_UART]->ops->write(iodevs[IODEV_UART]->opaque, buf, length);
}
if (do_lock)
spin_unlock(&console_lock);
return;
}
in_iodev++;
dprintf(" iodev_console_write() wp=%d\n", con_wp);
for (iodev_id_t id = 0; id < IODEV_MAX; id++) {
if (!iodevs[id])
continue;
if (!(iodevs[id]->usage & USAGE_CONSOLE)) {
/* Drop buffer */
con_rp[id] = con_wp + length;
continue;
}
if (!iodev_can_write(id))
continue;
if (con_wp > CONSOLE_BUFFER_SIZE)
con_rp[id] = max(con_wp - CONSOLE_BUFFER_SIZE, con_rp[id]);
dprintf(" rp=%d\n", con_rp[id]);
// Flush existing buffer to device if possible
while (con_rp[id] < con_wp) {
size_t buf_rp = con_rp[id] % CONSOLE_BUFFER_SIZE;
size_t block = min(con_wp - con_rp[id], CONSOLE_BUFFER_SIZE - buf_rp);
dprintf(" write buf %d\n", block);
ssize_t ret = iodev_write(id, &con_buf[buf_rp], block);
if (ret <= 0)
goto next_dev;
con_rp[id] += ret;
}
const u8 *p = buf;
size_t wrote = 0;
// Write the current buffer
while (wrote < length) {
ssize_t ret = iodev_write(id, p, length - wrote);
if (ret <= 0)
goto next_dev;
con_rp[id] += ret;
wrote += ret;
p += ret;
}
next_dev:;
}
// Update console buffer
if (length > CONSOLE_BUFFER_SIZE) {
buf += (length - CONSOLE_BUFFER_SIZE);
con_wp += (length - CONSOLE_BUFFER_SIZE);
length = CONSOLE_BUFFER_SIZE;
}
while (length) {
size_t buf_wp = con_wp % CONSOLE_BUFFER_SIZE;
size_t block = min(length, CONSOLE_BUFFER_SIZE - buf_wp);
memcpy(&con_buf[buf_wp], buf, block);
buf += block;
con_wp += block;
length -= block;
}
in_iodev--;
if (do_lock)
spin_unlock(&console_lock);
}
void iodev_handle_events(iodev_id_t id)
{
bool do_lock = mmu_active();
if (do_lock)
spin_lock(&console_lock);
if (in_iodev) {
if (do_lock)
spin_unlock(&console_lock);
return;
}
in_iodev++;
if (iodevs[id]->ops->handle_events)
iodevs[id]->ops->handle_events(iodevs[id]->opaque);
in_iodev--;
if (iodev_can_write(id))
iodev_console_write(NULL, 0);
if (do_lock)
spin_unlock(&console_lock);
}
void iodev_console_kick(void)
{
iodev_console_write(NULL, 0);
for (iodev_id_t id = 0; id < IODEV_MAX; id++) {
if (!iodevs[id])
continue;
if (!(iodevs[id]->usage & USAGE_CONSOLE))
continue;
iodev_handle_events(id);
}
}
void iodev_console_flush(void)
{
for (iodev_id_t id = 0; id < IODEV_MAX; id++) {
if (!iodevs[id])
continue;
if (!(iodevs[id]->usage & USAGE_CONSOLE))
continue;
iodev_flush(id);
}
}
void iodev_set_usage(iodev_id_t id, iodev_usage_t usage)
{
if (iodevs[id])
iodevs[id]->usage = usage;
}
iodev_usage_t iodev_get_usage(iodev_id_t id)
{
if (iodevs[id])
return iodevs[id]->usage;
return 0;
}
void *iodev_get_opaque(iodev_id_t id)
{
if (id >= IODEV_MAX || !iodevs[id])
return NULL;
return iodevs[id]->opaque;
}