altera_avgen.c revision 245376
1/*- 2 * Copyright (c) 2012-2013 Robert N. M. Watson 3 * All rights reserved. 4 * 5 * This software was developed by SRI International and the University of 6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) 7 * ("CTSRD"), as part of the DARPA CRASH research programme. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31#include <sys/cdefs.h> 32__FBSDID("$FreeBSD: head/sys/dev/altera/avgen/altera_avgen.c 245376 2013-01-13 16:43:59Z rwatson $"); 33 34#include <sys/param.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 <machine/bus.h> 49#include <machine/resource.h> 50#include <machine/vm.h> 51 52#include <vm/vm.h> 53 54#include <dev/altera/avgen/altera_avgen.h> 55 56/* 57 * Generic device driver for allowing read(), write(), and mmap() on 58 * memory-mapped, Avalon-attached devices. There is no actual dependence on 59 * Avalon, so conceivably this should just be soc_dev or similar, since many 60 * system-on-chip bus environments would work fine with the same code. 61 */ 62 63static d_mmap_t altera_avgen_mmap; 64static d_read_t altera_avgen_read; 65static d_write_t altera_avgen_write; 66 67static struct cdevsw avg_cdevsw = { 68 .d_version = D_VERSION, 69 .d_mmap = altera_avgen_mmap, 70 .d_read = altera_avgen_read, 71 .d_write = altera_avgen_write, 72 .d_name = "altera_avgen", 73}; 74 75static int 76altera_avgen_read(struct cdev *dev, struct uio *uio, int flag) 77{ 78 struct altera_avgen_softc *sc; 79 u_long offset, size; 80#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 229 230static int 231altera_avgen_process_options(struct altera_avgen_softc *sc, 232 const char *str_fileio, const char *str_mmapio, const char *str_devname, 233 int devunit) 234{ 235 const char *cp; 236 device_t dev = sc->avg_dev; 237 238 /* 239 * Check for valid combinations of options. 240 */ 241 if (str_fileio == NULL && str_mmapio == NULL) { 242 device_printf(dev, 243 "at least one of %s or %s must be specified\n", 244 ALTERA_AVALON_STR_FILEIO, ALTERA_AVALON_STR_MMAPIO); 245 return (ENXIO); 246 } 247 if (str_devname == NULL && devunit != -1) { 248 device_printf(dev, "%s requires %s be specified\n", 249 ALTERA_AVALON_STR_DEVUNIT, ALTERA_AVALON_STR_DEVNAME); 250 return (ENXIO); 251 } 252 253 /* 254 * Extract, digest, and save values. 255 */ 256 switch (sc->avg_width) { 257 case 1: 258 case 2: 259 case 4: 260#ifdef NOTYET 261 case 8: 262#endif 263 break; 264 265 default: 266 device_printf(dev, "%s unsupported value %u\n", 267 ALTERA_AVALON_STR_WIDTH, sc->avg_width); 268 return (ENXIO); 269 } 270 sc->avg_flags = 0; 271 if (str_fileio != NULL) { 272 for (cp = str_fileio; *cp != '\0'; cp++) { 273 switch (*cp) { 274 case ALTERA_AVALON_CHAR_READ: 275 sc->avg_flags |= ALTERA_AVALON_FLAG_READ; 276 break; 277 278 case ALTERA_AVALON_CHAR_WRITE: 279 sc->avg_flags |= ALTERA_AVALON_FLAG_WRITE; 280 break; 281 282 default: 283 device_printf(dev, 284 "invalid %s character %c\n", 285 ALTERA_AVALON_STR_FILEIO, *cp); 286 return (ENXIO); 287 } 288 } 289 } 290 if (str_mmapio != NULL) { 291 for (cp = str_mmapio; *cp != '\0'; cp++) { 292 switch (*cp) { 293 case ALTERA_AVALON_CHAR_READ: 294 sc->avg_flags |= ALTERA_AVALON_FLAG_MMAP_READ; 295 break; 296 297 case ALTERA_AVALON_CHAR_WRITE: 298 sc->avg_flags |= 299 ALTERA_AVALON_FLAG_MMAP_WRITE; 300 break; 301 302 case ALTERA_AVALON_CHAR_EXEC: 303 sc->avg_flags |= ALTERA_AVALON_FLAG_MMAP_EXEC; 304 break; 305 306 default: 307 device_printf(dev, 308 "invalid %s character %c\n", 309 ALTERA_AVALON_STR_MMAPIO, *cp); 310 return (ENXIO); 311 } 312 } 313 } 314 return (0); 315} 316 317int 318altera_avgen_attach(struct altera_avgen_softc *sc, const char *str_fileio, 319 const char *str_mmapio, const char *str_devname, int devunit) 320{ 321 device_t dev = sc->avg_dev; 322 char devname[SPECNAMELEN + 1]; 323 int error; 324 325 error = altera_avgen_process_options(sc, str_fileio, str_mmapio, 326 str_devname, devunit); 327 if (error) 328 return (error); 329 330 /* Select a device name. */ 331 if (str_devname != NULL) { 332 if (devunit != -1) 333 (void)snprintf(devname, sizeof(devname), "%s%d", 334 str_devname, devunit); 335 else 336 (void)snprintf(devname, sizeof(devname), "%s", 337 str_devname); 338 } else 339 snprintf(devname, sizeof(devname), "%s%d", "avgen", 340 sc->avg_unit); 341 342 if (rman_get_size(sc->avg_res) >= PAGE_SIZE || str_mmapio != NULL) { 343 if (rman_get_size(sc->avg_res) % PAGE_SIZE != 0) { 344 device_printf(dev, 345 "memory region not even multiple of page size\n"); 346 return (ENXIO); 347 } 348 if (rman_get_start(sc->avg_res) % PAGE_SIZE != 0) { 349 device_printf(dev, "memory region not page-aligned\n"); 350 return (ENXIO); 351 } 352 } 353 354 /* Device node allocation. */ 355 if (str_devname == NULL) { 356 str_devname = "altera_avgen%d"; 357 devunit = sc->avg_unit; 358 } 359 if (devunit != -1) 360 sc->avg_cdev = make_dev(&avg_cdevsw, sc->avg_unit, UID_ROOT, 361 GID_WHEEL, S_IRUSR | S_IWUSR, str_devname, devunit); 362 else 363 sc->avg_cdev = make_dev(&avg_cdevsw, sc->avg_unit, UID_ROOT, 364 GID_WHEEL, S_IRUSR | S_IWUSR, str_devname); 365 if (sc->avg_cdev == NULL) { 366 device_printf(sc->avg_dev, "%s: make_dev failed\n", __func__); 367 return (ENXIO); 368 } 369 /* XXXRW: Slight race between make_dev(9) and here. */ 370 sc->avg_cdev->si_drv1 = sc; 371 return (0); 372} 373 374void 375altera_avgen_detach(struct altera_avgen_softc *sc) 376{ 377 378 destroy_dev(sc->avg_cdev); 379} 380