altera_avgen_nexus.c revision 239674
181084Sjake/*- 281084Sjake * Copyright (c) 2012 Robert N. M. Watson 381084Sjake * All rights reserved. 481084Sjake * 581084Sjake * This software was developed by SRI International and the University of 681084Sjake * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) 781084Sjake * ("CTSRD"), as part of the DARPA CRASH research programme. 881084Sjake * 981084Sjake * Redistribution and use in source and binary forms, with or without 1081084Sjake * modification, are permitted provided that the following conditions 1191671Sume * are met: 1291671Sume * 1. Redistributions of source code must retain the above copyright 13104487Ssam * notice, this list of conditions and the following disclaimer. 14104487Ssam * 2. Redistributions in binary form must reproduce the above copyright 1590623Stmm * notice, this list of conditions and the following disclaimer in the 1686148Stmm * documentation and/or other materials provided with the distribution. 17105399Stmm * 1898371Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19100599Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20104519Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21104519Sphk * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2281084Sjake * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2393122Stmm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24111072Sjake * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25100399Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26111072Sjake * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27111072Sjake * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28111072Sjake * SUCH DAMAGE. 2986235Stmm */ 3093855Stmm 3193855Stmm#include <sys/cdefs.h> 3286235Stmm__FBSDID("$FreeBSD: head/sys/dev/altera/avgen/altera_avgen.c 239674 2012-08-25 11:07:43Z rwatson $"); 3386235Stmm 3486235Stmm#include <sys/param.h> 3590623Stmm#include <sys/bus.h> 3681084Sjake#include <sys/condvar.h> 3786235Stmm#include <sys/conf.h> 3886235Stmm#include <sys/kernel.h> 39112399Sjake#include <sys/lock.h> 4081084Sjake#include <sys/malloc.h> 4190623Stmm#include <sys/module.h> 4293264Sdillon#include <sys/mutex.h> 4390623Stmm#include <sys/rman.h> 4490623Stmm#include <sys/stat.h> 4590623Stmm#include <sys/systm.h> 4690623Stmm#include <sys/uio.h> 47105531Stmm 4881084Sjake#include <machine/bus.h> 49111684Sru#include <machine/resource.h> 5093840Stmm#include <machine/vm.h> 5193840Stmm 52111072Sjake#include <vm/vm.h> 5393840Stmm 5484202Sjake#include <dev/altera/avgen/altera_avgen.h> 5590623Stmm 56111684Sru/* 5781390Sjake * Generic device driver for allowing read(), write(), and mmap() on 5886235Stmm * memory-mapped, Avalon-attached devices. There is no actual dependence on 59111684Sru * Avalon, so conceivably this should just be soc_dev or similar, since many 6081084Sjake * system-on-chip bus environments would work fine with the same code. 6191970Stmm */ 62101070Sjake 63101070Sjakestatic d_mmap_t altera_avgen_mmap; 6489053Sjakestatic d_read_t altera_avgen_read; 6586235Stmmstatic d_write_t altera_avgen_write; 6693855Stmm 6786235Stmmstatic struct cdevsw avg_cdevsw = { 6881084Sjake .d_version = D_VERSION, 69100844Sjake .d_mmap = altera_avgen_mmap, 7082914Sjake .d_read = altera_avgen_read, 7186235Stmm .d_write = altera_avgen_write, 72112399Sjake .d_name = "altera_avgen", 73101070Sjake}; 7481084Sjake 75101070Sjakestatic int 7681390Sjakealtera_avgen_read(struct cdev *dev, struct uio *uio, int flag) 7796998Sjake{ 7881084Sjake struct altera_avgen_softc *sc; 7981084Sjake u_long offset, size; 8081084Sjake#ifdef NOTYET 81 uint64_t v8; 82#endif 83 uint32_t v4; 84 uint16_t v2; 85 uint8_t v1; 86 u_int width; 87 int error; 88 89 sc = dev->si_drv1; 90 if ((sc->avg_flags & ALTERA_AVALON_FLAG_READ) == 0) 91 return (EACCES); 92 width = sc->avg_width; 93 if (uio->uio_offset < 0 || uio->uio_offset % width != 0 || 94 uio->uio_resid % width != 0) 95 return (ENODEV); 96 size = rman_get_size(sc->avg_res); 97 if ((uio->uio_offset + uio->uio_resid < 0) || 98 (uio->uio_offset + uio->uio_resid > size)) 99 return (ENODEV); 100 while (uio->uio_resid > 0) { 101 offset = uio->uio_offset; 102 if (offset + width > size) 103 return (ENODEV); 104 switch (width) { 105 case 1: 106 v1 = bus_read_1(sc->avg_res, offset); 107 error = uiomove(&v1, sizeof(v1), uio); 108 break; 109 110 case 2: 111 v2 = bus_read_2(sc->avg_res, offset); 112 error = uiomove(&v2, sizeof(v2), uio); 113 break; 114 115 case 4: 116 v4 = bus_read_4(sc->avg_res, offset); 117 error = uiomove(&v4, sizeof(v4), uio); 118 break; 119 120#ifdef NOTYET 121 case 8: 122 v8 = bus_read_8(sc->avg_res, offset); 123 error = uiomove(&v8, sizeof(v8), uio); 124 break; 125 126#endif 127 128 default: 129 panic("%s: unexpected widthment %u", __func__, width); 130 } 131 if (error) 132 return (error); 133 } 134 return (0); 135} 136 137static int 138altera_avgen_write(struct cdev *dev, struct uio *uio, int flag) 139{ 140 struct altera_avgen_softc *sc; 141 u_long offset, size; 142#ifdef NOTYET 143 uint64_t v8; 144#endif 145 uint32_t v4; 146 uint16_t v2; 147 uint8_t v1; 148 u_int width; 149 int error; 150 151 sc = dev->si_drv1; 152 if ((sc->avg_flags & ALTERA_AVALON_FLAG_WRITE) == 0) 153 return (EACCES); 154 width = sc->avg_width; 155 if (uio->uio_offset < 0 || uio->uio_offset % width != 0 || 156 uio->uio_resid % width != 0) 157 return (ENODEV); 158 size = rman_get_size(sc->avg_res); 159 while (uio->uio_resid > 0) { 160 offset = uio->uio_offset; 161 if (offset + width > size) 162 return (ENODEV); 163 switch (width) { 164 case 1: 165 error = uiomove(&v1, sizeof(v1), uio); 166 if (error) 167 return (error); 168 bus_write_1(sc->avg_res, offset, v1); 169 break; 170 171 case 2: 172 error = uiomove(&v2, sizeof(v2), uio); 173 if (error) 174 return (error); 175 bus_write_2(sc->avg_res, offset, v2); 176 break; 177 178 case 4: 179 error = uiomove(&v4, sizeof(v4), uio); 180 if (error) 181 return (error); 182 bus_write_4(sc->avg_res, offset, v4); 183 break; 184 185#ifdef NOTYET 186 case 8: 187 error = uiomove(&v8, sizeof(v8), uio); 188 if (error) 189 return (error); 190 bus_write_8(sc->avg_res, offset, v8); 191 break; 192#endif 193 194 default: 195 panic("%s: unexpected width %u", __func__, width); 196 } 197 } 198 return (0); 199} 200 201static int 202altera_avgen_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr, 203 int nprot, vm_memattr_t *memattr) 204{ 205 struct altera_avgen_softc *sc; 206 207 sc = dev->si_drv1; 208 if (nprot & VM_PROT_READ) { 209 if ((sc->avg_flags & ALTERA_AVALON_FLAG_MMAP_READ) == 0) 210 return (EACCES); 211 } 212 if (nprot & VM_PROT_WRITE) { 213 if ((sc->avg_flags & ALTERA_AVALON_FLAG_MMAP_WRITE) == 0) 214 return (EACCES); 215 } 216 if (nprot & VM_PROT_EXECUTE) { 217 if ((sc->avg_flags & ALTERA_AVALON_FLAG_MMAP_EXEC) == 0) 218 return (EACCES); 219 } 220 if (trunc_page(offset) == offset && 221 rman_get_size(sc->avg_res) >= offset + PAGE_SIZE) { 222 *paddr = rman_get_start(sc->avg_res) + offset; 223 *memattr = VM_MEMATTR_UNCACHEABLE; 224 } else 225 return (ENODEV); 226 return (0); 227} 228 229static int 230altera_avgen_nexus_probe(device_t dev) 231{ 232 233 device_set_desc(dev, "Generic Altera Avalon device attachment"); 234 return (BUS_PROBE_DEFAULT); 235} 236 237static int 238altera_avgen_process_options(struct altera_avgen_softc *sc, 239 const char *str_fileio, const char *str_mmapio, const char *str_devname, 240 int devunit) 241{ 242 const char *cp; 243 device_t dev = sc->avg_dev; 244 245 /* 246 * Check for valid combinations of options. 247 */ 248 if (str_fileio == NULL && str_mmapio == NULL) { 249 device_printf(dev, 250 "at least one of %s or %s must be specified\n", 251 ALTERA_AVALON_STR_FILEIO, ALTERA_AVALON_STR_MMAPIO); 252 return (ENXIO); 253 } 254 if (str_devname == NULL && devunit != -1) { 255 device_printf(dev, "%s requires %s be specified\n", 256 ALTERA_AVALON_STR_DEVUNIT, ALTERA_AVALON_STR_DEVNAME); 257 return (ENXIO); 258 } 259 260 /* 261 * Extract, digest, and save values. 262 */ 263 switch (sc->avg_width) { 264 case 1: 265 case 2: 266 case 4: 267#ifdef NOTYET 268 case 8: 269#endif 270 break; 271 272 default: 273 device_printf(dev, "%s unsupported value %u\n", 274 ALTERA_AVALON_STR_WIDTH, sc->avg_width); 275 return (ENXIO); 276 } 277 sc->avg_flags = 0; 278 if (str_fileio != NULL) { 279 for (cp = str_fileio; *cp != '\0'; cp++) { 280 switch (*cp) { 281 case ALTERA_AVALON_CHAR_READ: 282 sc->avg_flags |= ALTERA_AVALON_FLAG_READ; 283 break; 284 285 case ALTERA_AVALON_CHAR_WRITE: 286 sc->avg_flags |= ALTERA_AVALON_FLAG_WRITE; 287 break; 288 289 default: 290 device_printf(dev, 291 "invalid %s character %c\n", 292 ALTERA_AVALON_STR_FILEIO, *cp); 293 return (ENXIO); 294 } 295 } 296 } 297 if (str_mmapio != NULL) { 298 for (cp = str_mmapio; *cp != '\0'; cp++) { 299 switch (*cp) { 300 case ALTERA_AVALON_CHAR_READ: 301 sc->avg_flags |= ALTERA_AVALON_FLAG_MMAP_READ; 302 break; 303 304 case ALTERA_AVALON_CHAR_WRITE: 305 sc->avg_flags |= 306 ALTERA_AVALON_FLAG_MMAP_WRITE; 307 break; 308 309 case ALTERA_AVALON_CHAR_EXEC: 310 sc->avg_flags |= ALTERA_AVALON_FLAG_MMAP_EXEC; 311 break; 312 313 default: 314 device_printf(dev, 315 "invalid %s character %c\n", 316 ALTERA_AVALON_STR_MMAPIO, *cp); 317 return (ENXIO); 318 } 319 } 320 } 321 return (0); 322} 323 324static int 325altera_avgen_nexus_attach(device_t dev) 326{ 327 struct altera_avgen_softc *sc; 328 const char *str_fileio, *str_mmapio; 329 const char *str_devname; 330 char devname[SPECNAMELEN + 1]; 331 int devunit, error; 332 333 sc = device_get_softc(dev); 334 sc->avg_dev = dev; 335 sc->avg_unit = device_get_unit(dev); 336 337 /* 338 * Query non-standard hints to find out what operations are permitted 339 * on the device, and whether it is cached. 340 */ 341 str_fileio = NULL; 342 str_mmapio = NULL; 343 str_devname = NULL; 344 devunit = -1; 345 sc->avg_width = 1; 346 error = resource_int_value(device_get_name(dev), device_get_unit(dev), 347 ALTERA_AVALON_STR_WIDTH, &sc->avg_width); 348 if (error != 0 && error != ENOENT) { 349 device_printf(dev, "invalid %s\n", ALTERA_AVALON_STR_WIDTH); 350 return (error); 351 } 352 (void)resource_string_value(device_get_name(dev), 353 device_get_unit(dev), ALTERA_AVALON_STR_FILEIO, &str_fileio); 354 (void)resource_string_value(device_get_name(dev), 355 device_get_unit(dev), ALTERA_AVALON_STR_MMAPIO, &str_mmapio); 356 (void)resource_string_value(device_get_name(dev), 357 device_get_unit(dev), ALTERA_AVALON_STR_DEVNAME, &str_devname); 358 (void)resource_int_value(device_get_name(dev), device_get_unit(dev), 359 ALTERA_AVALON_STR_DEVUNIT, &devunit); 360 error = altera_avgen_process_options(sc, str_fileio, str_mmapio, 361 str_devname, devunit); 362 if (error) 363 return (error); 364 365 /* Select a device name. */ 366 if (str_devname != NULL) { 367 if (devunit != -1) 368 (void)snprintf(devname, sizeof(devname), "%s%d", 369 str_devname, devunit); 370 else 371 (void)snprintf(devname, sizeof(devname), "%s", 372 str_devname); 373 } else 374 snprintf(devname, sizeof(devname), "%s%d", "avgen", 375 sc->avg_unit); 376 377 /* Memory allocation and checking. */ 378 sc->avg_rid = 0; 379 sc->avg_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 380 &sc->avg_rid, RF_ACTIVE); 381 if (sc->avg_res == NULL) { 382 device_printf(dev, "couldn't map memory\n"); 383 return (ENXIO); 384 } 385 if (rman_get_size(sc->avg_res) >= PAGE_SIZE || str_mmapio != NULL) { 386 if (rman_get_size(sc->avg_res) % PAGE_SIZE != 0) { 387 device_printf(dev, 388 "memory region not even multiple of page size\n"); 389 error = ENXIO; 390 goto error; 391 } 392 if (rman_get_start(sc->avg_res) % PAGE_SIZE != 0) { 393 device_printf(dev, "memory region not page-aligned\n"); 394 error = ENXIO; 395 goto error; 396 } 397 } 398 399 /* Device node allocation. */ 400 if (str_devname == NULL) { 401 str_devname = "altera_avgen%d"; 402 devunit = sc->avg_unit; 403 } 404 if (devunit != -1) 405 sc->avg_cdev = make_dev(&avg_cdevsw, sc->avg_unit, UID_ROOT, 406 GID_WHEEL, S_IRUSR | S_IWUSR, str_devname, devunit); 407 else 408 sc->avg_cdev = make_dev(&avg_cdevsw, sc->avg_unit, UID_ROOT, 409 GID_WHEEL, S_IRUSR | S_IWUSR, str_devname); 410 if (sc->avg_cdev == NULL) { 411 device_printf(sc->avg_dev, "%s: make_dev failed\n", __func__); 412 error = ENXIO; 413 goto error; 414 } 415 /* XXXRW: Slight race between make_dev(9) and here. */ 416 sc->avg_cdev->si_drv1 = sc; 417 return (0); 418 419error: 420 bus_release_resource(dev, SYS_RES_MEMORY, sc->avg_rid, sc->avg_res); 421 return (error); 422} 423 424static int 425altera_avgen_nexus_detach(device_t dev) 426{ 427 struct altera_avgen_softc *sc; 428 429 sc = device_get_softc(dev); 430 destroy_dev(sc->avg_cdev); 431 bus_release_resource(dev, SYS_RES_MEMORY, sc->avg_rid, sc->avg_res); 432 return (0); 433} 434 435static device_method_t altera_avgen_nexus_methods[] = { 436 DEVMETHOD(device_probe, altera_avgen_nexus_probe), 437 DEVMETHOD(device_attach, altera_avgen_nexus_attach), 438 DEVMETHOD(device_detach, altera_avgen_nexus_detach), 439 { 0, 0 } 440}; 441 442static driver_t altera_avgen_nexus_driver = { 443 "altera_avgen", 444 altera_avgen_nexus_methods, 445 sizeof(struct altera_avgen_softc), 446}; 447 448static devclass_t altera_avgen_devclass; 449 450DRIVER_MODULE(avgen, nexus, altera_avgen_nexus_driver, altera_avgen_devclass, 451 0, 0); 452