Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*-
 * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com>
 * All rights reserved.
 *
 * This software was developed by SRI International and the University of
 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
 * ("CTSRD"), as part of the DARPA CRASH research programme.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * SRI-Cambridge BERI soft processor <-> ARM core ring buffer.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/malloc.h>
#include <sys/rman.h>
#include <sys/timeet.h>
#include <sys/timetc.h>
#include <sys/conf.h>
#include <sys/uio.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/event.h>
#include <sys/selinfo.h>

#include <dev/fdt/fdt_common.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>

#include <machine/bus.h>
#include <machine/fdt.h>
#include <machine/cpu.h>
#include <machine/intr.h>

#define READ4(_sc, _reg) \
	bus_read_4((_sc)->res[0], _reg)
#define WRITE4(_sc, _reg, _val) \
	bus_write_4((_sc)->res[0], _reg, _val)

#define CDES_INT_EN		(1 << 15)
#define CDES_CAUSE_MASK		0x3
#define CDES_CAUSE_SHIFT	13
#define DEVNAME_MAXLEN		256

typedef struct
{
	uint16_t cdes;
	uint16_t interrupt_level;
	uint16_t in;
	uint16_t out;
} control_reg_t;

struct beri_softc {
	struct resource		*res[3];
	bus_space_tag_t		bst;
	bus_space_handle_t	bsh;
	struct cdev		*cdev;
	device_t		dev;
	void			*read_ih;
	void			*write_ih;
	struct selinfo		beri_rsel;
	struct mtx		beri_mtx;
	int			opened;

	char			devname[DEVNAME_MAXLEN];
	int			control_read;
	int			control_write;
	int			data_read;
	int			data_write;
	int			data_size;
};

static struct resource_spec beri_spec[] = {
	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
	{ SYS_RES_IRQ,		0,	RF_ACTIVE },
	{ SYS_RES_IRQ,		1,	RF_ACTIVE },
	{ -1, 0 }
};

static control_reg_t
get_control_reg(struct beri_softc *sc, int dir)
{
	uint32_t offset;
	uint16_t dst[4];
	control_reg_t c;
	uint16_t *cp;
	int i;

	cp = (uint16_t *)&c;

	offset = dir ? sc->control_write : sc->control_read;
	((uint32_t *)dst)[0] = READ4(sc, offset);
	((uint32_t *)dst)[1] = READ4(sc, offset + 4);

	for (i = 0; i < 4; i++)
		cp[i] = dst[3 - i];

	return (c);
}

static void
set_control_reg(struct beri_softc *sc, int dir, control_reg_t *c)
{
	uint32_t offset;
	uint16_t src[4];
	uint16_t *cp;
	int i;

	cp = (uint16_t *)c;

	for (i = 0; i < 4; i++)
		src[3 - i] = cp[i];

	offset = dir ? sc->control_write : sc->control_read;
	WRITE4(sc, offset + 0, ((uint32_t *)src)[0]);
	WRITE4(sc, offset + 4, ((uint32_t *)src)[1]);
}

static int
get_stock(struct beri_softc *sc, int dir, control_reg_t *c)
{
	uint32_t fill;

	fill = (c->in - c->out + sc->data_size) % sc->data_size;

	if (dir)
		return (sc->data_size - fill - 1);
	else
		return (fill);
}

static void
beri_intr_write(void *arg)
{
	struct beri_softc *sc;
	control_reg_t c;

	sc = arg;

	c = get_control_reg(sc, 1);
	if (c.cdes & CDES_INT_EN) {
		c.cdes &= ~(CDES_INT_EN);
		set_control_reg(sc, 1, &c);
	}

	mtx_lock(&sc->beri_mtx);
	selwakeuppri(&sc->beri_rsel, PZERO + 1);
	KNOTE_LOCKED(&sc->beri_rsel.si_note, 0);
	mtx_unlock(&sc->beri_mtx);
}

static void
beri_intr_read(void *arg)
{
	struct beri_softc *sc;
	control_reg_t c;

	sc = arg;

	c = get_control_reg(sc, 0);
	if (c.cdes & CDES_INT_EN) {
		c.cdes &= ~(CDES_INT_EN);
		set_control_reg(sc, 0, &c);
	}

	mtx_lock(&sc->beri_mtx);
	selwakeuppri(&sc->beri_rsel, PZERO + 1);
	KNOTE_LOCKED(&sc->beri_rsel.si_note, 0);
	mtx_unlock(&sc->beri_mtx);
}

static int
beri_open(struct cdev *dev, int flags __unused,
    int fmt __unused, struct thread *td __unused)
{
	struct beri_softc *sc;
	control_reg_t c;

	sc = dev->si_drv1;

	if (sc->opened)
		return (1);

	/* Setup interrupt handlers */
	if (bus_setup_intr(sc->dev, sc->res[1], INTR_TYPE_BIO | INTR_MPSAFE,
		NULL, beri_intr_read, sc, &sc->read_ih)) {
		device_printf(sc->dev, "Unable to setup read intr\n");
		return (1);
	}
	if (bus_setup_intr(sc->dev, sc->res[2], INTR_TYPE_BIO | INTR_MPSAFE,
		NULL, beri_intr_write, sc, &sc->write_ih)) {
		device_printf(sc->dev, "Unable to setup write intr\n");
		return (1);
	}

	sc->opened = 1;

	/* Clear write buffer */
	c = get_control_reg(sc, 1);
	c.in = c.out;
	c.cdes = 0;
	set_control_reg(sc, 1, &c);

	/* Clear read buffer */
	c = get_control_reg(sc, 0);
	c.out = c.in;
	c.cdes = 0;
	set_control_reg(sc, 0, &c);

	return (0);
}

static int
beri_close(struct cdev *dev, int flags __unused,
    int fmt __unused, struct thread *td __unused)
{
	struct beri_softc *sc;

	sc = dev->si_drv1;

	if (sc->opened) {
		sc->opened = 0;

		/* Unsetup interrupt handlers */
		bus_teardown_intr(sc->dev, sc->res[1], sc->read_ih);
		bus_teardown_intr(sc->dev, sc->res[2], sc->write_ih);
	}

	return (0);
}

static int
beri_rdwr(struct cdev *dev, struct uio *uio, int ioflag)
{
	struct beri_softc *sc;
	uint32_t offset;
	control_reg_t c;
	uint16_t *ptr;
	uint8_t *dst;
	int stock;
	int dir;
	int amount;
	int count;

	sc = dev->si_drv1;

	dir = uio->uio_rw ? 1 : 0;

	c = get_control_reg(sc, dir);
	stock = get_stock(sc, dir, &c);
	if (stock < uio->uio_resid) {
		device_printf(sc->dev, "Err: no data/space available\n");
		return (1);
	}

	amount = uio->uio_resid;
	ptr = dir ? &c.in : &c.out;
	count = (sc->data_size - *ptr);

	offset = dir ? sc->data_write : sc->data_read;
	dst = (uint8_t *)(sc->bsh + offset);

	if (amount <= count) {
		uiomove(dst + *ptr, amount, uio);
	} else {
		uiomove(dst + *ptr, count, uio);
		uiomove(dst, (amount - count), uio);
	}

	*ptr = (*ptr + amount) % sc->data_size;
	set_control_reg(sc, dir, &c);

	return (0);
}

static int
beri_kqread(struct knote *kn, long hint)
{
	struct beri_softc *sc;
	control_reg_t c;
	int stock;

	sc = kn->kn_hook;

	c = get_control_reg(sc, 0);
	stock = get_stock(sc, 0, &c);
	if (stock) {
		kn->kn_data = stock;
		return (1);
	}

	kn->kn_data = 0;

	/* Wait at least one new byte in buffer */
	c.interrupt_level = 1;

	/* Enable interrupts */
	c.cdes |= (CDES_INT_EN);
	set_control_reg(sc, 0, &c);

	return (0);
}

static int
beri_kqwrite(struct knote *kn, long hint)
{
	struct beri_softc *sc;
	control_reg_t c;
	int stock;

	sc = kn->kn_hook;

	c = get_control_reg(sc, 1);
	stock = get_stock(sc, 1, &c);
	if (stock) {
		kn->kn_data = stock;
		return (1);
	}

	kn->kn_data = 0;

	/* Wait at least one free position in buffer */
	c.interrupt_level = sc->data_size - 2;

	/* Enable interrupts */
	c.cdes |= (CDES_INT_EN);
	set_control_reg(sc, 1, &c);

	return (0);
}

static void
beri_kqdetach(struct knote *kn)
{
	struct beri_softc *sc;

	sc = kn->kn_hook;

	knlist_remove(&sc->beri_rsel.si_note, kn, 0);
}

static struct filterops beri_read_filterops = {
	.f_isfd =       1,
	.f_attach =     NULL,
	.f_detach =     beri_kqdetach,
	.f_event =      beri_kqread,
};

static struct filterops beri_write_filterops = {
	.f_isfd =       1,
	.f_attach =     NULL,
	.f_detach =     beri_kqdetach,
	.f_event =      beri_kqwrite,
};

static int
beri_kqfilter(struct cdev *dev, struct knote *kn)
{
	struct beri_softc *sc;

	sc = dev->si_drv1;

	switch(kn->kn_filter) {
	case EVFILT_READ:
		kn->kn_fop = &beri_read_filterops;
		break;
	case EVFILT_WRITE:
		kn->kn_fop = &beri_write_filterops;
		break;
	default:
		return(EINVAL);
	}

	kn->kn_hook = sc;
	knlist_add(&sc->beri_rsel.si_note, kn, 0);

	return (0);
}

static struct cdevsw beri_cdevsw = {
	.d_version =	D_VERSION,
	.d_open =	beri_open,
	.d_close =	beri_close,
	.d_write =	beri_rdwr,
	.d_read =	beri_rdwr,
	.d_kqfilter =	beri_kqfilter,
	.d_name =	"beri ring buffer",
};

static int
parse_fdt(struct beri_softc *sc)
{
	pcell_t dts_value[0];
	phandle_t node;
	int len;

	if ((node = ofw_bus_get_node(sc->dev)) == -1)
		return (ENXIO);

	/* get device name */
	if (OF_getprop(ofw_bus_get_node(sc->dev), "device_name",
		&sc->devname, sizeof(sc->devname)) <= 0) {
		device_printf(sc->dev, "Can't get device_name\n");
		return (ENXIO);
	}

	if ((len = OF_getproplen(node, "data_size")) <= 0)
		return (ENXIO);
	OF_getencprop(node, "data_size", dts_value, len);
	sc->data_size = dts_value[0];

	if ((len = OF_getproplen(node, "data_read")) <= 0)
		return (ENXIO);
	OF_getencprop(node, "data_read", dts_value, len);
	sc->data_read = dts_value[0];

	if ((len = OF_getproplen(node, "data_write")) <= 0)
		return (ENXIO);
	OF_getencprop(node, "data_write", dts_value, len);
	sc->data_write = dts_value[0];

	if ((len = OF_getproplen(node, "control_read")) <= 0)
		return (ENXIO);
	OF_getencprop(node, "control_read", dts_value, len);
	sc->control_read = dts_value[0];

	if ((len = OF_getproplen(node, "control_write")) <= 0)
		return (ENXIO);
	OF_getencprop(node, "control_write", dts_value, len);
	sc->control_write = dts_value[0];

	return (0);
}

static int
beri_probe(device_t dev)
{

	if (!ofw_bus_status_okay(dev))
		return (ENXIO);

	if (!ofw_bus_is_compatible(dev, "sri-cambridge,beri-ring"))
		return (ENXIO);

	device_set_desc(dev, "SRI-Cambridge BERI ring buffer");
	return (BUS_PROBE_DEFAULT);
}

static int
beri_attach(device_t dev)
{
	struct beri_softc *sc;

	sc = device_get_softc(dev);
	sc->dev = dev;

	if (bus_alloc_resources(dev, beri_spec, sc->res)) {
		device_printf(dev, "could not allocate resources\n");
		return (ENXIO);
	}

	/* Memory interface */
	sc->bst = rman_get_bustag(sc->res[0]);
	sc->bsh = rman_get_bushandle(sc->res[0]);

	if (parse_fdt(sc)) {
		device_printf(sc->dev, "Can't get FDT values\n");
		return (ENXIO);
	}

	sc->cdev = make_dev(&beri_cdevsw, 0, UID_ROOT, GID_WHEEL,
	    S_IRWXU, "%s", sc->devname);
	if (sc->cdev == NULL) {
		device_printf(dev, "Failed to create character device.\n");
		return (ENXIO);
	}

	sc->cdev->si_drv1 = sc;

	mtx_init(&sc->beri_mtx, "beri_mtx", NULL, MTX_DEF);
	knlist_init_mtx(&sc->beri_rsel.si_note, &sc->beri_mtx);

	return (0);
}

static device_method_t beri_methods[] = {
	DEVMETHOD(device_probe,		beri_probe),
	DEVMETHOD(device_attach,	beri_attach),
	{ 0, 0 }
};

static driver_t beri_driver = {
	"beri_ring",
	beri_methods,
	sizeof(struct beri_softc),
};

static devclass_t beri_devclass;

DRIVER_MODULE(beri_ring, simplebus, beri_driver, beri_devclass, 0, 0);