2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012-2013, 2016 Robert N. M. Watson
7 * This software was developed by SRI International and the University of
8 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
9 * ("CTSRD"), as part of the DARPA CRASH research programme.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
39 #include <sys/condvar.h>
41 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/mutex.h>
48 #include <sys/systm.h>
51 #include <geom/geom_disk.h>
53 #include <machine/bus.h>
54 #include <machine/resource.h>
58 #include <dev/altera/avgen/altera_avgen.h>
61 * Generic device driver for allowing read(), write(), and mmap() on
62 * memory-mapped, Avalon-attached devices. There is no actual dependence on
63 * Avalon, so conceivably this should just be soc_dev or similar, since many
64 * system-on-chip bus environments would work fine with the same code.
67 devclass_t altera_avgen_devclass;
69 static d_mmap_t altera_avgen_mmap;
70 static d_read_t altera_avgen_read;
71 static d_write_t altera_avgen_write;
73 #define ALTERA_AVGEN_DEVNAME "altera_avgen"
74 #define ALTERA_AVGEN_DEVNAME_FMT (ALTERA_AVGEN_DEVNAME "%d")
76 static struct cdevsw avg_cdevsw = {
77 .d_version = D_VERSION,
78 .d_mmap = altera_avgen_mmap,
79 .d_read = altera_avgen_read,
80 .d_write = altera_avgen_write,
81 .d_name = ALTERA_AVGEN_DEVNAME,
84 #define ALTERA_AVGEN_SECTORSIZE 512 /* Not configurable at this time. */
87 altera_avgen_read(struct cdev *dev, struct uio *uio, int flag)
89 struct altera_avgen_softc *sc;
101 if ((sc->avg_flags & ALTERA_AVALON_FLAG_READ) == 0)
103 width = sc->avg_width;
104 if (uio->uio_offset < 0 || uio->uio_offset % width != 0 ||
105 uio->uio_resid % width != 0)
107 size = rman_get_size(sc->avg_res);
108 if ((uio->uio_offset + uio->uio_resid < 0) ||
109 (uio->uio_offset + uio->uio_resid > size))
111 while (uio->uio_resid > 0) {
112 offset = uio->uio_offset;
113 if (offset + width > size)
117 v1 = bus_read_1(sc->avg_res, offset);
118 error = uiomove(&v1, sizeof(v1), uio);
122 v2 = bus_read_2(sc->avg_res, offset);
123 error = uiomove(&v2, sizeof(v2), uio);
127 v4 = bus_read_4(sc->avg_res, offset);
128 error = uiomove(&v4, sizeof(v4), uio);
133 v8 = bus_read_8(sc->avg_res, offset);
134 error = uiomove(&v8, sizeof(v8), uio);
140 panic("%s: unexpected widthment %u", __func__, width);
149 altera_avgen_write(struct cdev *dev, struct uio *uio, int flag)
151 struct altera_avgen_softc *sc;
163 if ((sc->avg_flags & ALTERA_AVALON_FLAG_WRITE) == 0)
165 width = sc->avg_width;
166 if (uio->uio_offset < 0 || uio->uio_offset % width != 0 ||
167 uio->uio_resid % width != 0)
169 size = rman_get_size(sc->avg_res);
170 while (uio->uio_resid > 0) {
171 offset = uio->uio_offset;
172 if (offset + width > size)
176 error = uiomove(&v1, sizeof(v1), uio);
179 bus_write_1(sc->avg_res, offset, v1);
183 error = uiomove(&v2, sizeof(v2), uio);
186 bus_write_2(sc->avg_res, offset, v2);
190 error = uiomove(&v4, sizeof(v4), uio);
193 bus_write_4(sc->avg_res, offset, v4);
198 error = uiomove(&v8, sizeof(v8), uio);
201 bus_write_8(sc->avg_res, offset, v8);
206 panic("%s: unexpected width %u", __func__, width);
213 altera_avgen_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
214 int nprot, vm_memattr_t *memattr)
216 struct altera_avgen_softc *sc;
219 if (nprot & VM_PROT_READ) {
220 if ((sc->avg_flags & ALTERA_AVALON_FLAG_MMAP_READ) == 0)
223 if (nprot & VM_PROT_WRITE) {
224 if ((sc->avg_flags & ALTERA_AVALON_FLAG_MMAP_WRITE) == 0)
227 if (nprot & VM_PROT_EXECUTE) {
228 if ((sc->avg_flags & ALTERA_AVALON_FLAG_MMAP_EXEC) == 0)
231 if (trunc_page(offset) == offset &&
232 rman_get_size(sc->avg_res) >= offset + PAGE_SIZE) {
233 *paddr = rman_get_start(sc->avg_res) + offset;
234 *memattr = VM_MEMATTR_UNCACHEABLE;
241 * NB: We serialise block reads and writes in case the OS is generating
242 * concurrent I/O against the same block, in which case we want one I/O (or
243 * another) to win. This is not sufficient to provide atomicity for the
244 * sector in the presence of a fail stop -- however, we're just writing this
245 * to non-persistent DRAM .. right?
248 altera_avgen_disk_strategy(struct bio *bp)
250 struct altera_avgen_softc *sc;
255 sc = bp->bio_disk->d_drv1;
257 bcount = bp->bio_bcount;
258 pblkno = bp->bio_pblkno;
261 * Serialize block reads / writes.
263 mtx_lock(&sc->avg_disk_mtx);
264 switch (bp->bio_cmd) {
266 if (!(sc->avg_flags & ALTERA_AVALON_FLAG_GEOM_READ)) {
267 biofinish(bp, NULL, EIO);
270 switch (sc->avg_width) {
272 bus_read_region_1(sc->avg_res,
273 bp->bio_pblkno * ALTERA_AVGEN_SECTORSIZE,
274 (uint8_t *)data, bcount);
278 bus_read_region_2(sc->avg_res,
279 bp->bio_pblkno * ALTERA_AVGEN_SECTORSIZE,
280 (uint16_t *)data, bcount / 2);
284 bus_read_region_4(sc->avg_res,
285 bp->bio_pblkno * ALTERA_AVGEN_SECTORSIZE,
286 (uint32_t *)data, bcount / 4);
290 panic("%s: unexpected width %u", __func__,
296 if (!(sc->avg_flags & ALTERA_AVALON_FLAG_GEOM_WRITE)) {
297 biofinish(bp, NULL, EROFS);
300 switch (sc->avg_width) {
302 bus_write_region_1(sc->avg_res,
303 bp->bio_pblkno * ALTERA_AVGEN_SECTORSIZE,
304 (uint8_t *)data, bcount);
308 bus_write_region_2(sc->avg_res,
309 bp->bio_pblkno * ALTERA_AVGEN_SECTORSIZE,
310 (uint16_t *)data, bcount / 2);
314 bus_write_region_4(sc->avg_res,
315 bp->bio_pblkno * ALTERA_AVGEN_SECTORSIZE,
316 (uint32_t *)data, bcount / 4);
320 panic("%s: unexpected width %u", __func__,
326 panic("%s: unsupported I/O operation %d", __func__,
329 mtx_unlock(&sc->avg_disk_mtx);
330 biofinish(bp, NULL, 0);
334 altera_avgen_process_options(struct altera_avgen_softc *sc,
335 const char *str_fileio, const char *str_geomio, const char *str_mmapio,
336 const char *str_devname, int devunit)
339 device_t dev = sc->avg_dev;
342 * Check for valid combinations of options.
344 if (str_fileio == NULL && str_geomio == NULL && str_mmapio == NULL) {
346 "at least one of %s, %s, or %s must be specified\n",
347 ALTERA_AVALON_STR_FILEIO, ALTERA_AVALON_STR_GEOMIO,
348 ALTERA_AVALON_STR_MMAPIO);
353 * Validity check: a device can either be a GEOM device (in which case
354 * we use GEOM to register the device node), or a special device --
355 * but not both as that causes a collision in /dev.
357 if (str_geomio != NULL && (str_fileio != NULL || str_mmapio != NULL)) {
359 "at most one of %s and (%s or %s) may be specified\n",
360 ALTERA_AVALON_STR_GEOMIO, ALTERA_AVALON_STR_FILEIO,
361 ALTERA_AVALON_STR_MMAPIO);
366 * Ensure that a unit is specified if a name is also specified.
368 if (str_devname == NULL && devunit != -1) {
369 device_printf(dev, "%s requires %s be specified\n",
370 ALTERA_AVALON_STR_DEVUNIT, ALTERA_AVALON_STR_DEVNAME);
375 * Extract, digest, and save values.
377 switch (sc->avg_width) {
387 device_printf(dev, "%s unsupported value %u\n",
388 ALTERA_AVALON_STR_WIDTH, sc->avg_width);
392 if (str_fileio != NULL) {
393 for (cp = str_fileio; *cp != '\0'; cp++) {
395 case ALTERA_AVALON_CHAR_READ:
396 sc->avg_flags |= ALTERA_AVALON_FLAG_READ;
399 case ALTERA_AVALON_CHAR_WRITE:
400 sc->avg_flags |= ALTERA_AVALON_FLAG_WRITE;
405 "invalid %s character %c\n",
406 ALTERA_AVALON_STR_FILEIO, *cp);
411 if (str_geomio != NULL) {
412 for (cp = str_geomio; *cp != '\0'; cp++){
414 case ALTERA_AVALON_CHAR_READ:
415 sc->avg_flags |= ALTERA_AVALON_FLAG_GEOM_READ;
418 case ALTERA_AVALON_CHAR_WRITE:
419 sc->avg_flags |= ALTERA_AVALON_FLAG_GEOM_WRITE;
424 "invalid %s character %c\n",
425 ALTERA_AVALON_STR_GEOMIO, *cp);
430 if (str_mmapio != NULL) {
431 for (cp = str_mmapio; *cp != '\0'; cp++) {
433 case ALTERA_AVALON_CHAR_READ:
434 sc->avg_flags |= ALTERA_AVALON_FLAG_MMAP_READ;
437 case ALTERA_AVALON_CHAR_WRITE:
439 ALTERA_AVALON_FLAG_MMAP_WRITE;
442 case ALTERA_AVALON_CHAR_EXEC:
443 sc->avg_flags |= ALTERA_AVALON_FLAG_MMAP_EXEC;
448 "invalid %s character %c\n",
449 ALTERA_AVALON_STR_MMAPIO, *cp);
458 altera_avgen_attach(struct altera_avgen_softc *sc, const char *str_fileio,
459 const char *str_geomio, const char *str_mmapio, const char *str_devname,
462 device_t dev = sc->avg_dev;
465 error = altera_avgen_process_options(sc, str_fileio, str_geomio,
466 str_mmapio, str_devname, devunit);
470 if (rman_get_size(sc->avg_res) >= PAGE_SIZE || str_mmapio != NULL) {
471 if (rman_get_size(sc->avg_res) % PAGE_SIZE != 0) {
473 "memory region not even multiple of page size\n");
476 if (rman_get_start(sc->avg_res) % PAGE_SIZE != 0) {
477 device_printf(dev, "memory region not page-aligned\n");
483 * If a GEOM permission is requested, then create the device via GEOM.
484 * Otherwise, create a special device. We checked during options
485 * processing that both weren't requested a once.
487 if (str_devname != NULL) {
488 sc->avg_name = strdup(str_devname, M_TEMP);
489 devunit = sc->avg_unit;
491 sc->avg_name = strdup(ALTERA_AVGEN_DEVNAME, M_TEMP);
492 if (sc->avg_flags & (ALTERA_AVALON_FLAG_GEOM_READ |
493 ALTERA_AVALON_FLAG_GEOM_WRITE)) {
494 mtx_init(&sc->avg_disk_mtx, "altera_avgen_disk", NULL,
496 sc->avg_disk = disk_alloc();
497 sc->avg_disk->d_drv1 = sc;
498 sc->avg_disk->d_strategy = altera_avgen_disk_strategy;
501 sc->avg_disk->d_name = sc->avg_name;
502 sc->avg_disk->d_unit = devunit;
505 * NB: As avg_res is a multiple of PAGE_SIZE, it is also a
506 * multiple of ALTERA_AVGEN_SECTORSIZE.
508 sc->avg_disk->d_sectorsize = ALTERA_AVGEN_SECTORSIZE;
509 sc->avg_disk->d_mediasize = rman_get_size(sc->avg_res);
510 sc->avg_disk->d_maxsize = ALTERA_AVGEN_SECTORSIZE;
511 disk_create(sc->avg_disk, DISK_VERSION);
513 /* Device node allocation. */
514 if (str_devname == NULL) {
515 str_devname = ALTERA_AVGEN_DEVNAME_FMT;
516 devunit = sc->avg_unit;
519 sc->avg_cdev = make_dev(&avg_cdevsw, sc->avg_unit,
520 UID_ROOT, GID_WHEEL, S_IRUSR | S_IWUSR, "%s%d",
521 str_devname, devunit);
523 sc->avg_cdev = make_dev(&avg_cdevsw, sc->avg_unit,
524 UID_ROOT, GID_WHEEL, S_IRUSR | S_IWUSR,
526 if (sc->avg_cdev == NULL) {
527 device_printf(sc->avg_dev, "%s: make_dev failed\n",
532 /* XXXRW: Slight race between make_dev(9) and here. */
533 sc->avg_cdev->si_drv1 = sc;
539 altera_avgen_detach(struct altera_avgen_softc *sc)
542 KASSERT((sc->avg_disk != NULL) || (sc->avg_cdev != NULL),
543 ("%s: neither GEOM nor special device", __func__));
545 if (sc->avg_disk != NULL) {
546 disk_gone(sc->avg_disk);
547 disk_destroy(sc->avg_disk);
548 free(sc->avg_name, M_TEMP);
549 mtx_destroy(&sc->avg_disk_mtx);
551 destroy_dev(sc->avg_cdev);