summaryrefslogtreecommitdiff
path: root/src/drivers/ao_bufio.c
blob: 70e30b67fee98897f22dd473b114b72063d991dc (plain) (blame)
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
320
321
/*
 * Copyright © 2013 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#ifndef AO_FAT_TEST
#include "ao.h"
#endif

/* Include bufio commands */
#ifndef AO_FAT_TEST
#define BUFIO_COMMANDS	0
#endif

#include "ao_sdcard.h"
#include "ao_bufio.h"

#define AO_NUM_BUF		16
#define AO_BUFSIZ		512

struct ao_bufio {
	uint32_t	block;
	int16_t		seqno;
	uint8_t		busy;	
	uint8_t		dirty;
};

static struct ao_bufio	ao_bufio[AO_NUM_BUF];
static uint8_t		ao_buffer[AO_NUM_BUF][AO_BUFSIZ];
static int16_t		ao_seqno;
static uint8_t		ao_bufio_mutex;

#if 0
#define DBG(...) printf(__VA_ARGS__)
#else
#define DBG(...) (void) 0
#endif

static inline void
ao_bufio_lock(void)
{
	ao_mutex_get(&ao_bufio_mutex);
}

static inline void
ao_bufio_unlock(void)
{
	ao_mutex_put(&ao_bufio_mutex);
}

static inline int16_t
ao_seqno_age(int16_t s)
{
	return ao_seqno - s;
}

static inline int16_t
ao_seqno_next(void)
{
	return ++ao_seqno;
}

static inline int
ao_seqno_older(int16_t a, int16_t b)
{
	return ao_seqno_age(a) > ao_seqno_age(b);
}

static inline void
ao_validate_bufno(int b)
{
	if (b < 0 || AO_NUM_BUF <= b)
		ao_panic(AO_PANIC_BUFIO);
}

static inline int
ao_buf_to_num(uint8_t *buf)
{
	int b = (buf - &ao_buffer[0][0]) / AO_BUFSIZ;

	ao_validate_bufno(b);
	return b;
}

static inline int
ao_bufio_to_num(struct ao_bufio *bufio)
{
	int b = (bufio - ao_bufio);

	ao_validate_bufno(b);
	return b;
}

static inline struct ao_bufio *
ao_buf_to_bufio(uint8_t *buf)
{
	int b = ao_buf_to_num(buf);
	struct ao_bufio *bufio;

	bufio = &ao_bufio[b];
	DBG ("buf %08x is %d bufio %08x\n", buf, b, bufio);
	return bufio;
}

static inline uint8_t *
ao_bufio_to_buf(struct ao_bufio *bufio)
{
	int b = ao_bufio_to_num(bufio);
	uint8_t *buf;

	buf = &ao_buffer[b][0];
	DBG ("bufio %08x is %d buf %08x\n", bufio, b, buf);
	return buf;
}

/*
 * Write a buffer to storage if it is dirty
 */
static void
ao_bufio_write(struct ao_bufio *bufio)
{
	if (bufio->dirty) {
		ao_sdcard_write_block(bufio->block, ao_bufio_to_buf(bufio));
		bufio->dirty = 0;
	}
}

/*
 * Read a buffer from storage
 */
static uint8_t
ao_bufio_read(struct ao_bufio *bufio)
{
	uint8_t	*buf = ao_bufio_to_buf(bufio);

	return ao_sdcard_read_block(bufio->block, buf);
}

/*
 * Find a buffer containing the specified block
 */
static struct ao_bufio *
ao_bufio_find_block(uint32_t block)
{
	int b;

	for (b = 0; b < AO_NUM_BUF; b++) {
		struct ao_bufio *bufio = &ao_bufio[b];
		if (bufio->block == block) {
			DBG ("Found existing buffer %d (seqno %d)\n",
				ao_bufio_to_num(bufio), bufio->seqno);
			return bufio;
		}
	}
	return NULL;
}

/*
 * Find the least recently used idle buffer
 */
static struct ao_bufio *
ao_bufio_find_idle(void)
{
	int b;
	struct ao_bufio *oldest = NULL;

	for (b = 0; b < AO_NUM_BUF; b++) {
		struct ao_bufio *bufio = &ao_bufio[b];
		if (!bufio->busy)
			if (!oldest || ao_seqno_older(bufio->seqno, oldest->seqno))
				oldest = bufio;
	}
	if (oldest)
		DBG ("Using idle buffer %d (seqno %d)\n",
			ao_bufio_to_num(oldest), oldest->seqno);
	return oldest;
}

/*
 * Return a pointer to a buffer containing
 * the contents of the specified block
 */
uint8_t *
ao_bufio_get(uint32_t block)
{
	struct ao_bufio *bufio;
	uint8_t	*buf = NULL;

	ao_bufio_lock();
	bufio = ao_bufio_find_block(block);
	if (!bufio) {
		bufio = ao_bufio_find_idle();
		if (bufio) {
			ao_bufio_write(bufio);
			bufio->block = block;
			DBG ("read buffer\n");
			if (!ao_bufio_read(bufio)) {
				bufio->block = 0xffffffff;
				bufio = NULL;
			}
		} else
			ao_panic(AO_PANIC_BUFIO);
	}
	if (bufio) {
		bufio->busy++;
		if (!bufio->busy)
			ao_panic(AO_PANIC_BUFIO);
		buf = ao_bufio_to_buf(bufio);
	}
	ao_bufio_unlock();
	return buf;
}

/*
 * Release a buffer, marking it dirty
 * if it has been written to
 */
void
ao_bufio_put(uint8_t *buf, uint8_t write)
{
	struct ao_bufio *bufio;

	ao_bufio_lock();
	bufio = ao_buf_to_bufio(buf);
	
	if (!bufio->busy)
		ao_panic(AO_PANIC_BUFIO);

	DBG ("idle buffer %d write %d\n", ao_bufio_to_num(bufio), write);
	bufio->dirty |= write;
	if (!--bufio->busy) {
		bufio->seqno = ao_seqno_next();
		DBG ("not busy, seqno %d\n", bufio->seqno);
	}
	ao_bufio_unlock();
}

/*
 * Flush a single buffer immediately. Useful
 * if write order is important
 */
void
ao_bufio_flush_one(uint8_t *buf)
{
	ao_bufio_lock();
	ao_bufio_write(ao_buf_to_bufio(buf));
	ao_bufio_unlock();
}

/*
 * Flush all buffers to storage
 */
void
ao_bufio_flush(void)
{
	int	b;

	ao_bufio_lock();
	for (b = 0; b < AO_NUM_BUF; b++)
		ao_bufio_write(&ao_bufio[b]);
	ao_bufio_unlock();
}

#if BUFIO_COMMANDS
static void
ao_bufio_test_read(void)
{
	uint8_t	*buf;
	ao_cmd_decimal();
	if (ao_cmd_status != ao_cmd_success)
		return;
	if ((buf = ao_bufio_get(ao_cmd_lex_u32))) {
		int i;
		for (i = 0; i < 512; i++) {
			printf (" %02x", buf[i]);
			if ((i & 0xf) == 0xf)
				printf("\n");
		}
		ao_bufio_put(buf, 0);
	}
}

static const struct ao_cmds ao_bufio_cmds[] = {
	{ ao_bufio_test_read,	"q\0Test bufio read" },
	{ 0, NULL },
};
#endif

void
ao_bufio_setup(void)
{
	int b;

	for (b = 0; b < AO_NUM_BUF; b++) {
		ao_bufio[b].dirty = 0;
		ao_bufio[b].busy = 0;
		ao_bufio[b].block = 0xffffffff;
	}
}

void
ao_bufio_init(void)
{
	ao_bufio_setup();
	ao_sdcard_init();
#if BUFIO_COMMANDS
	ao_cmd_register(&ao_bufio_cmds[0]);
#endif
}