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