xref: /freebsd/sys/dev/altera/avgen/altera_avgen.c (revision fdafd315)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2012-2013, 2016 Robert N. M. Watson
5  * All rights reserved.
6  *
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.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/bio.h>
35 #include <sys/bus.h>
36 #include <sys/condvar.h>
37 #include <sys/conf.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/rman.h>
44 #include <sys/stat.h>
45 #include <sys/systm.h>
46 #include <sys/uio.h>
47 
48 #include <geom/geom_disk.h>
49 
50 #include <machine/bus.h>
51 #include <machine/resource.h>
52 
53 #include <vm/vm.h>
54 
55 #include <dev/altera/avgen/altera_avgen.h>
56 
57 /*
58  * Generic device driver for allowing read(), write(), and mmap() on
59  * memory-mapped, Avalon-attached devices.  There is no actual dependence on
60  * Avalon, so conceivably this should just be soc_dev or similar, since many
61  * system-on-chip bus environments would work fine with the same code.
62  */
63 
64 static d_mmap_t altera_avgen_mmap;
65 static d_read_t altera_avgen_read;
66 static d_write_t altera_avgen_write;
67 
68 #define	ALTERA_AVGEN_DEVNAME		"altera_avgen"
69 #define	ALTERA_AVGEN_DEVNAME_FMT	(ALTERA_AVGEN_DEVNAME "%d")
70 
71 static struct cdevsw avg_cdevsw = {
72 	.d_version =	D_VERSION,
73 	.d_mmap =	altera_avgen_mmap,
74 	.d_read =	altera_avgen_read,
75 	.d_write =	altera_avgen_write,
76 	.d_name =	ALTERA_AVGEN_DEVNAME,
77 };
78 
79 #define	ALTERA_AVGEN_SECTORSIZE	512	/* Not configurable at this time. */
80 
81 static int
altera_avgen_read(struct cdev * dev,struct uio * uio,int flag)82 altera_avgen_read(struct cdev *dev, struct uio *uio, int flag)
83 {
84 	struct altera_avgen_softc *sc;
85 	u_long offset, size;
86 #ifdef NOTYET
87 	uint64_t v8;
88 #endif
89 	uint32_t v4;
90 	uint16_t v2;
91 	uint8_t v1;
92 	u_int width;
93 	int error;
94 
95 	sc = dev->si_drv1;
96 	if ((sc->avg_flags & ALTERA_AVALON_FLAG_READ) == 0)
97 		return (EACCES);
98 	width = sc->avg_width;
99 	if (uio->uio_offset < 0 || uio->uio_offset % width != 0 ||
100 	    uio->uio_resid % width != 0)
101 		return (ENODEV);
102 	size = rman_get_size(sc->avg_res);
103 	if ((uio->uio_offset + uio->uio_resid < 0) ||
104 	    (uio->uio_offset + uio->uio_resid > size))
105 		return (ENODEV);
106 	while (uio->uio_resid > 0) {
107 		offset = uio->uio_offset;
108 		if (offset + width > size)
109 			return (ENODEV);
110 		switch (width) {
111 		case 1:
112 			v1 = bus_read_1(sc->avg_res, offset);
113 			error = uiomove(&v1, sizeof(v1), uio);
114 			break;
115 
116 		case 2:
117 			v2 = bus_read_2(sc->avg_res, offset);
118 			error = uiomove(&v2, sizeof(v2), uio);
119 			break;
120 
121 		case 4:
122 			v4 = bus_read_4(sc->avg_res, offset);
123 			error = uiomove(&v4, sizeof(v4), uio);
124 			break;
125 
126 #ifdef NOTYET
127 		case 8:
128 			v8 = bus_read_8(sc->avg_res, offset);
129 			error = uiomove(&v8, sizeof(v8), uio);
130 			break;
131 
132 #endif
133 
134 		default:
135 			panic("%s: unexpected widthment %u", __func__, width);
136 		}
137 		if (error)
138 			return (error);
139 	}
140 	return (0);
141 }
142 
143 static int
altera_avgen_write(struct cdev * dev,struct uio * uio,int flag)144 altera_avgen_write(struct cdev *dev, struct uio *uio, int flag)
145 {
146 	struct altera_avgen_softc *sc;
147 	u_long offset, size;
148 #ifdef NOTYET
149 	uint64_t v8;
150 #endif
151 	uint32_t v4;
152 	uint16_t v2;
153 	uint8_t v1;
154 	u_int width;
155 	int error;
156 
157 	sc = dev->si_drv1;
158 	if ((sc->avg_flags & ALTERA_AVALON_FLAG_WRITE) == 0)
159 		return (EACCES);
160 	width = sc->avg_width;
161 	if (uio->uio_offset < 0 || uio->uio_offset % width != 0 ||
162 	    uio->uio_resid % width != 0)
163 		return (ENODEV);
164 	size = rman_get_size(sc->avg_res);
165 	while (uio->uio_resid > 0) {
166 		offset = uio->uio_offset;
167 		if (offset + width > size)
168 			return (ENODEV);
169 		switch (width) {
170 		case 1:
171 			error = uiomove(&v1, sizeof(v1), uio);
172 			if (error)
173 				return (error);
174 			bus_write_1(sc->avg_res, offset, v1);
175 			break;
176 
177 		case 2:
178 			error = uiomove(&v2, sizeof(v2), uio);
179 			if (error)
180 				return (error);
181 			bus_write_2(sc->avg_res, offset, v2);
182 			break;
183 
184 		case 4:
185 			error = uiomove(&v4, sizeof(v4), uio);
186 			if (error)
187 				return (error);
188 			bus_write_4(sc->avg_res, offset, v4);
189 			break;
190 
191 #ifdef NOTYET
192 		case 8:
193 			error = uiomove(&v8, sizeof(v8), uio);
194 			if (error)
195 				return (error);
196 			bus_write_8(sc->avg_res, offset, v8);
197 			break;
198 #endif
199 
200 		default:
201 			panic("%s: unexpected width %u", __func__, width);
202 		}
203 	}
204 	return (0);
205 }
206 
207 static int
altera_avgen_mmap(struct cdev * dev,vm_ooffset_t offset,vm_paddr_t * paddr,int nprot,vm_memattr_t * memattr)208 altera_avgen_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
209     int nprot, vm_memattr_t *memattr)
210 {
211 	struct altera_avgen_softc *sc;
212 
213 	sc = dev->si_drv1;
214 	if (nprot & VM_PROT_READ) {
215 		if ((sc->avg_flags & ALTERA_AVALON_FLAG_MMAP_READ) == 0)
216 			return (EACCES);
217 	}
218 	if (nprot & VM_PROT_WRITE) {
219 		if ((sc->avg_flags & ALTERA_AVALON_FLAG_MMAP_WRITE) == 0)
220 			return (EACCES);
221 	}
222 	if (nprot & VM_PROT_EXECUTE) {
223 		if ((sc->avg_flags & ALTERA_AVALON_FLAG_MMAP_EXEC) == 0)
224 			return (EACCES);
225 	}
226 	if (trunc_page(offset) == offset &&
227 	    offset + PAGE_SIZE > offset &&
228 	    rman_get_size(sc->avg_res) >= offset + PAGE_SIZE) {
229 		*paddr = rman_get_start(sc->avg_res) + offset;
230 		*memattr = VM_MEMATTR_UNCACHEABLE;
231 	} else
232 		return (ENODEV);
233 	return (0);
234 }
235 
236 /*
237  * NB: We serialise block reads and writes in case the OS is generating
238  * concurrent I/O against the same block, in which case we want one I/O (or
239  * another) to win.  This is not sufficient to provide atomicity for the
240  * sector in the presence of a fail stop -- however, we're just writing this
241  * to non-persistent DRAM .. right?
242  */
243 static void
altera_avgen_disk_strategy(struct bio * bp)244 altera_avgen_disk_strategy(struct bio *bp)
245 {
246 	struct altera_avgen_softc *sc;
247 	void *data;
248 	long bcount;
249 	daddr_t pblkno;
250 	int error;
251 
252 	sc = bp->bio_disk->d_drv1;
253 	data = bp->bio_data;
254 	bcount = bp->bio_bcount;
255 	pblkno = bp->bio_pblkno;
256 	error = 0;
257 
258 	/*
259 	 * Serialize block reads / writes.
260 	 */
261 	mtx_lock(&sc->avg_disk_mtx);
262 	switch (bp->bio_cmd) {
263 	case BIO_READ:
264 		if (!(sc->avg_flags & ALTERA_AVALON_FLAG_GEOM_READ)) {
265 			error = EROFS;
266 			break;
267 		}
268 		switch (sc->avg_width) {
269 		case 1:
270 			bus_read_region_1(sc->avg_res,
271 			    bp->bio_pblkno * ALTERA_AVGEN_SECTORSIZE,
272 			    (uint8_t *)data, bcount);
273 			break;
274 
275 		case 2:
276 			bus_read_region_2(sc->avg_res,
277 			    bp->bio_pblkno * ALTERA_AVGEN_SECTORSIZE,
278 			    (uint16_t *)data, bcount / 2);
279 			break;
280 
281 		case 4:
282 			bus_read_region_4(sc->avg_res,
283 			    bp->bio_pblkno * ALTERA_AVGEN_SECTORSIZE,
284 			    (uint32_t *)data, bcount / 4);
285 			break;
286 
287 		default:
288 			panic("%s: unexpected width %u", __func__,
289 			    sc->avg_width);
290 		}
291 		break;
292 
293 	case BIO_WRITE:
294 		if (!(sc->avg_flags & ALTERA_AVALON_FLAG_GEOM_WRITE)) {
295 			biofinish(bp, NULL, EROFS);
296 			break;
297 		}
298 		switch (sc->avg_width) {
299 		case 1:
300 			bus_write_region_1(sc->avg_res,
301 			    bp->bio_pblkno * ALTERA_AVGEN_SECTORSIZE,
302 			    (uint8_t *)data, bcount);
303 			break;
304 
305 		case 2:
306 			bus_write_region_2(sc->avg_res,
307 			    bp->bio_pblkno * ALTERA_AVGEN_SECTORSIZE,
308 			    (uint16_t *)data, bcount / 2);
309 			break;
310 
311 		case 4:
312 			bus_write_region_4(sc->avg_res,
313 			    bp->bio_pblkno * ALTERA_AVGEN_SECTORSIZE,
314 			    (uint32_t *)data, bcount / 4);
315 			break;
316 
317 		default:
318 			panic("%s: unexpected width %u", __func__,
319 			    sc->avg_width);
320 		}
321 		break;
322 
323 	default:
324 		error = EOPNOTSUPP;
325 		break;
326 	}
327 	mtx_unlock(&sc->avg_disk_mtx);
328 	biofinish(bp, NULL, error);
329 }
330 
331 static int
altera_avgen_process_options(struct altera_avgen_softc * sc,const char * str_fileio,const char * str_geomio,const char * str_mmapio,const char * str_devname,int devunit)332 altera_avgen_process_options(struct altera_avgen_softc *sc,
333     const char *str_fileio, const char *str_geomio, const char *str_mmapio,
334     const char *str_devname, int devunit)
335 {
336 	const char *cp;
337 	device_t dev = sc->avg_dev;
338 
339 	/*
340 	 * Check for valid combinations of options.
341 	 */
342 	if (str_fileio == NULL && str_geomio == NULL && str_mmapio == NULL) {
343 		device_printf(dev,
344 		    "at least one of %s, %s, or %s must be specified\n",
345 		    ALTERA_AVALON_STR_FILEIO, ALTERA_AVALON_STR_GEOMIO,
346 		    ALTERA_AVALON_STR_MMAPIO);
347 		return (ENXIO);
348 	}
349 
350 	/*
351 	 * Validity check: a device can either be a GEOM device (in which case
352 	 * we use GEOM to register the device node), or a special device --
353 	 * but not both as that causes a collision in /dev.
354 	 */
355 	if (str_geomio != NULL && (str_fileio != NULL || str_mmapio != NULL)) {
356 		device_printf(dev,
357 		    "at most one of %s and (%s or %s) may be specified\n",
358 		    ALTERA_AVALON_STR_GEOMIO, ALTERA_AVALON_STR_FILEIO,
359 		    ALTERA_AVALON_STR_MMAPIO);
360 		return (ENXIO);
361 	}
362 
363 	/*
364 	 * Ensure that a unit is specified if a name is also specified.
365 	 */
366 	if (str_devname == NULL && devunit != -1) {
367 		device_printf(dev, "%s requires %s be specified\n",
368 		    ALTERA_AVALON_STR_DEVUNIT, ALTERA_AVALON_STR_DEVNAME);
369 		return (ENXIO);
370 	}
371 
372 	/*
373 	 * Extract, digest, and save values.
374 	 */
375 	switch (sc->avg_width) {
376 	case 1:
377 	case 2:
378 	case 4:
379 #ifdef NOTYET
380 	case 8:
381 #endif
382 		break;
383 
384 	default:
385 		device_printf(dev, "%s unsupported value %u\n",
386 		    ALTERA_AVALON_STR_WIDTH, sc->avg_width);
387 		return (ENXIO);
388 	}
389 	sc->avg_flags = 0;
390 	if (str_fileio != NULL) {
391 		for (cp = str_fileio; *cp != '\0'; cp++) {
392 			switch (*cp) {
393 			case ALTERA_AVALON_CHAR_READ:
394 				sc->avg_flags |= ALTERA_AVALON_FLAG_READ;
395 				break;
396 
397 			case ALTERA_AVALON_CHAR_WRITE:
398 				sc->avg_flags |= ALTERA_AVALON_FLAG_WRITE;
399 				break;
400 
401 			default:
402 				device_printf(dev,
403 				    "invalid %s character %c\n",
404 				    ALTERA_AVALON_STR_FILEIO, *cp);
405 				return (ENXIO);
406 			}
407 		}
408 	}
409 	if (str_geomio != NULL) {
410 		for (cp = str_geomio; *cp != '\0'; cp++){
411 			switch (*cp) {
412 			case ALTERA_AVALON_CHAR_READ:
413 				sc->avg_flags |= ALTERA_AVALON_FLAG_GEOM_READ;
414 				break;
415 
416 			case ALTERA_AVALON_CHAR_WRITE:
417 				sc->avg_flags |= ALTERA_AVALON_FLAG_GEOM_WRITE;
418 				break;
419 
420 			default:
421 				device_printf(dev,
422 				    "invalid %s character %c\n",
423 				    ALTERA_AVALON_STR_GEOMIO, *cp);
424 				return (ENXIO);
425 			}
426 		}
427 	}
428 	if (str_mmapio != NULL) {
429 		for (cp = str_mmapio; *cp != '\0'; cp++) {
430 			switch (*cp) {
431 			case ALTERA_AVALON_CHAR_READ:
432 				sc->avg_flags |= ALTERA_AVALON_FLAG_MMAP_READ;
433 				break;
434 
435 			case ALTERA_AVALON_CHAR_WRITE:
436 				sc->avg_flags |=
437 				    ALTERA_AVALON_FLAG_MMAP_WRITE;
438 				break;
439 
440 			case ALTERA_AVALON_CHAR_EXEC:
441 				sc->avg_flags |= ALTERA_AVALON_FLAG_MMAP_EXEC;
442 				break;
443 
444 			default:
445 				device_printf(dev,
446 				    "invalid %s character %c\n",
447 				    ALTERA_AVALON_STR_MMAPIO, *cp);
448 				return (ENXIO);
449 			}
450 		}
451 	}
452 	return (0);
453 }
454 
455 int
altera_avgen_attach(struct altera_avgen_softc * sc,const char * str_fileio,const char * str_geomio,const char * str_mmapio,const char * str_devname,int devunit)456 altera_avgen_attach(struct altera_avgen_softc *sc, const char *str_fileio,
457     const char *str_geomio, const char *str_mmapio, const char *str_devname,
458     int devunit)
459 {
460 	device_t dev = sc->avg_dev;
461 	int error;
462 
463 	error = altera_avgen_process_options(sc, str_fileio, str_geomio,
464 	    str_mmapio, str_devname, devunit);
465 	if (error)
466 		return (error);
467 
468 	if (rman_get_size(sc->avg_res) >= PAGE_SIZE || str_mmapio != NULL) {
469 		if (rman_get_size(sc->avg_res) % PAGE_SIZE != 0) {
470 			device_printf(dev,
471 			    "memory region not even multiple of page size\n");
472 			return (ENXIO);
473 		}
474 		if (rman_get_start(sc->avg_res) % PAGE_SIZE != 0) {
475 			device_printf(dev, "memory region not page-aligned\n");
476 			return (ENXIO);
477 		}
478 	}
479 
480 	/*
481 	 * If a GEOM permission is requested, then create the device via GEOM.
482 	 * Otherwise, create a special device.  We checked during options
483 	 * processing that both weren't requested a once.
484 	 */
485 	if (str_devname != NULL) {
486 		sc->avg_name = strdup(str_devname, M_TEMP);
487 		devunit = sc->avg_unit;
488 	} else
489 		sc->avg_name = strdup(ALTERA_AVGEN_DEVNAME, M_TEMP);
490 	if (sc->avg_flags & (ALTERA_AVALON_FLAG_GEOM_READ |
491 	    ALTERA_AVALON_FLAG_GEOM_WRITE)) {
492 		mtx_init(&sc->avg_disk_mtx, "altera_avgen_disk", NULL,
493 		    MTX_DEF);
494 		sc->avg_disk = disk_alloc();
495 		sc->avg_disk->d_drv1 = sc;
496 		sc->avg_disk->d_strategy = altera_avgen_disk_strategy;
497 		if (devunit == -1)
498 			devunit = 0;
499 		sc->avg_disk->d_name = sc->avg_name;
500 		sc->avg_disk->d_unit = devunit;
501 
502 		/*
503 		 * NB: As avg_res is a multiple of PAGE_SIZE, it is also a
504 		 * multiple of ALTERA_AVGEN_SECTORSIZE.
505 		 */
506 		sc->avg_disk->d_sectorsize = ALTERA_AVGEN_SECTORSIZE;
507 		sc->avg_disk->d_mediasize = rman_get_size(sc->avg_res);
508 		sc->avg_disk->d_maxsize = ALTERA_AVGEN_SECTORSIZE;
509 		disk_create(sc->avg_disk, DISK_VERSION);
510 	} else {
511 		/* Device node allocation. */
512 		if (str_devname == NULL) {
513 			str_devname = ALTERA_AVGEN_DEVNAME_FMT;
514 			devunit = sc->avg_unit;
515 		}
516 		if (devunit != -1)
517 			sc->avg_cdev = make_dev(&avg_cdevsw, sc->avg_unit,
518 			    UID_ROOT, GID_WHEEL, S_IRUSR | S_IWUSR, "%s%d",
519 			    str_devname, devunit);
520 		else
521 			sc->avg_cdev = make_dev(&avg_cdevsw, sc->avg_unit,
522 			    UID_ROOT, GID_WHEEL, S_IRUSR | S_IWUSR,
523 			    "%s", str_devname);
524 		if (sc->avg_cdev == NULL) {
525 			device_printf(sc->avg_dev, "%s: make_dev failed\n",
526 			    __func__);
527 			return (ENXIO);
528 		}
529 
530 		/* XXXRW: Slight race between make_dev(9) and here. */
531 		sc->avg_cdev->si_drv1 = sc;
532 	}
533 	return (0);
534 }
535 
536 void
altera_avgen_detach(struct altera_avgen_softc * sc)537 altera_avgen_detach(struct altera_avgen_softc *sc)
538 {
539 
540 	KASSERT((sc->avg_disk != NULL) || (sc->avg_cdev != NULL),
541 	    ("%s: neither GEOM nor special device", __func__));
542 
543 	if (sc->avg_disk != NULL) {
544 		disk_gone(sc->avg_disk);
545 		disk_destroy(sc->avg_disk);
546 		free(sc->avg_name, M_TEMP);
547 		mtx_destroy(&sc->avg_disk_mtx);
548 	} else {
549 		destroy_dev(sc->avg_cdev);
550 	}
551 }
552