1 /*- 2 * Copyright (c) 1988 University of Utah. 3 * Copyright (c) 1982, 1986, 1990 The Regents of the University of California. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * the Systems Programming Group of the University of Utah Computer 8 * Science Department, and code derived from software contributed to 9 * Berkeley by William Jolitz. 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 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * from: Utah $Hdr: mem.c 1.13 89/10/08$ 36 * from: @(#)mem.c 7.2 (Berkeley) 5/9/91 37 */ 38 39 #include <sys/cdefs.h> 40 __FBSDID("$FreeBSD$"); 41 42 /* 43 * Memory special file 44 */ 45 46 #include <sys/param.h> 47 #include <sys/conf.h> 48 #include <sys/fcntl.h> 49 #include <sys/kernel.h> 50 #include <sys/lock.h> 51 #include <sys/ioccom.h> 52 #include <sys/malloc.h> 53 #include <sys/memrange.h> 54 #include <sys/module.h> 55 #include <sys/mutex.h> 56 #include <sys/proc.h> 57 #include <sys/msgbuf.h> 58 #include <sys/systm.h> 59 #include <sys/signalvar.h> 60 #include <sys/uio.h> 61 62 #include <machine/md_var.h> 63 #include <machine/vmparam.h> 64 65 #include <vm/vm.h> 66 #include <vm/pmap.h> 67 #include <vm/vm_extern.h> 68 #include <vm/vm_page.h> 69 70 #include <machine/memdev.h> 71 72 static void ppc_mrinit(struct mem_range_softc *); 73 static int ppc_mrset(struct mem_range_softc *, struct mem_range_desc *, int *); 74 75 MALLOC_DEFINE(M_MEMDESC, "memdesc", "memory range descriptors"); 76 77 static struct mem_range_ops ppc_mem_range_ops = { 78 ppc_mrinit, 79 ppc_mrset, 80 NULL, 81 NULL 82 }; 83 struct mem_range_softc mem_range_softc = { 84 &ppc_mem_range_ops, 85 0, 0, NULL 86 }; 87 88 /* ARGSUSED */ 89 int 90 memrw(struct cdev *dev, struct uio *uio, int flags) 91 { 92 struct iovec *iov; 93 int error = 0; 94 vm_offset_t va, eva, off, v; 95 vm_prot_t prot; 96 struct vm_page m; 97 vm_page_t marr; 98 vm_size_t cnt; 99 100 cnt = 0; 101 error = 0; 102 103 GIANT_REQUIRED; 104 105 while (uio->uio_resid > 0 && !error) { 106 iov = uio->uio_iov; 107 if (iov->iov_len == 0) { 108 uio->uio_iov++; 109 uio->uio_iovcnt--; 110 if (uio->uio_iovcnt < 0) 111 panic("memrw"); 112 continue; 113 } 114 if (dev2unit(dev) == CDEV_MINOR_MEM) { 115 kmem_direct_mapped: v = uio->uio_offset; 116 117 off = uio->uio_offset & PAGE_MASK; 118 cnt = PAGE_SIZE - ((vm_offset_t)iov->iov_base & 119 PAGE_MASK); 120 cnt = min(cnt, PAGE_SIZE - off); 121 cnt = min(cnt, iov->iov_len); 122 123 if (mem_valid(v, cnt)) { 124 error = EFAULT; 125 break; 126 } 127 128 if (!pmap_dev_direct_mapped(v, cnt)) { 129 error = uiomove((void *)v, cnt, uio); 130 } else { 131 m.phys_addr = trunc_page(v); 132 marr = &m; 133 error = uiomove_fromphys(&marr, off, cnt, uio); 134 } 135 } 136 else if (dev2unit(dev) == CDEV_MINOR_KMEM) { 137 va = uio->uio_offset; 138 139 if ((va < VM_MIN_KERNEL_ADDRESS) || (va > virtual_end)) 140 goto kmem_direct_mapped; 141 142 va = trunc_page(uio->uio_offset); 143 eva = round_page(uio->uio_offset 144 + iov->iov_len); 145 146 /* 147 * Make sure that all the pages are currently resident 148 * so that we don't create any zero-fill pages. 149 */ 150 151 for (; va < eva; va += PAGE_SIZE) 152 if (pmap_extract(kernel_pmap, va) == 0) 153 return (EFAULT); 154 155 prot = (uio->uio_rw == UIO_READ) 156 ? VM_PROT_READ : VM_PROT_WRITE; 157 158 va = uio->uio_offset; 159 if (kernacc((void *) va, iov->iov_len, prot) 160 == FALSE) 161 return (EFAULT); 162 163 error = uiomove((void *)va, iov->iov_len, uio); 164 165 continue; 166 } 167 } 168 169 return (error); 170 } 171 172 /* 173 * allow user processes to MMAP some memory sections 174 * instead of going through read/write 175 */ 176 int 177 memmmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr, 178 int prot, vm_memattr_t *memattr) 179 { 180 int i; 181 182 /* 183 * /dev/mem is the only one that makes sense through this 184 * interface. For /dev/kmem any physaddr we return here 185 * could be transient and hence incorrect or invalid at 186 * a later time. 187 */ 188 if (dev2unit(dev) != CDEV_MINOR_MEM) 189 return (-1); 190 191 /* Only direct-mapped addresses. */ 192 if (mem_valid(offset, 0) 193 && pmap_dev_direct_mapped(offset, 0)) 194 return (EFAULT); 195 196 *paddr = offset; 197 198 for (i = 0; i < mem_range_softc.mr_ndesc; i++) { 199 if (!(mem_range_softc.mr_desc[i].mr_flags & MDF_ACTIVE)) 200 continue; 201 202 if (offset >= mem_range_softc.mr_desc[i].mr_base && 203 offset < mem_range_softc.mr_desc[i].mr_base + 204 mem_range_softc.mr_desc[i].mr_len) { 205 switch (mem_range_softc.mr_desc[i].mr_flags & 206 MDF_ATTRMASK) { 207 case MDF_WRITEBACK: 208 *memattr = VM_MEMATTR_WRITE_BACK; 209 break; 210 case MDF_WRITECOMBINE: 211 *memattr = VM_MEMATTR_WRITE_COMBINING; 212 break; 213 case MDF_UNCACHEABLE: 214 *memattr = VM_MEMATTR_UNCACHEABLE; 215 break; 216 case MDF_WRITETHROUGH: 217 *memattr = VM_MEMATTR_WRITE_THROUGH; 218 break; 219 } 220 221 break; 222 } 223 } 224 225 return (0); 226 } 227 228 static void 229 ppc_mrinit(struct mem_range_softc *sc) 230 { 231 sc->mr_cap = 0; 232 sc->mr_ndesc = 8; /* XXX: Should be dynamically expandable */ 233 sc->mr_desc = malloc(sc->mr_ndesc * sizeof(struct mem_range_desc), 234 M_MEMDESC, M_NOWAIT | M_ZERO); 235 if (sc->mr_desc == NULL) 236 panic("%s: malloc returns NULL", __func__); 237 } 238 239 static int 240 ppc_mrset(struct mem_range_softc *sc, struct mem_range_desc *desc, int *arg) 241 { 242 int i; 243 244 switch(*arg) { 245 case MEMRANGE_SET_UPDATE: 246 for (i = 0; i < sc->mr_ndesc; i++) { 247 if (!sc->mr_desc[i].mr_len) { 248 sc->mr_desc[i] = *desc; 249 sc->mr_desc[i].mr_flags |= MDF_ACTIVE; 250 return (0); 251 } 252 if (sc->mr_desc[i].mr_base == desc->mr_base && 253 sc->mr_desc[i].mr_len == desc->mr_len) 254 return (EEXIST); 255 } 256 return (ENOSPC); 257 case MEMRANGE_SET_REMOVE: 258 for (i = 0; i < sc->mr_ndesc; i++) 259 if (sc->mr_desc[i].mr_base == desc->mr_base && 260 sc->mr_desc[i].mr_len == desc->mr_len) { 261 bzero(&sc->mr_desc[i], sizeof(sc->mr_desc[i])); 262 return (0); 263 } 264 return (ENOENT); 265 default: 266 return (EOPNOTSUPP); 267 } 268 269 return (0); 270 } 271 272 /* 273 * Operations for changing memory attributes. 274 * 275 * This is basically just an ioctl shim for mem_range_attr_get 276 * and mem_range_attr_set. 277 */ 278 /* ARGSUSED */ 279 int 280 memioctl(struct cdev *dev __unused, u_long cmd, caddr_t data, int flags, 281 struct thread *td) 282 { 283 int nd, error = 0; 284 struct mem_range_op *mo = (struct mem_range_op *)data; 285 struct mem_range_desc *md; 286 287 /* is this for us? */ 288 if ((cmd != MEMRANGE_GET) && 289 (cmd != MEMRANGE_SET)) 290 return (ENOTTY); 291 292 /* any chance we can handle this? */ 293 if (mem_range_softc.mr_op == NULL) 294 return (EOPNOTSUPP); 295 296 /* do we have any descriptors? */ 297 if (mem_range_softc.mr_ndesc == 0) 298 return (ENXIO); 299 300 switch (cmd) { 301 case MEMRANGE_GET: 302 nd = imin(mo->mo_arg[0], mem_range_softc.mr_ndesc); 303 if (nd > 0) { 304 md = (struct mem_range_desc *) 305 malloc(nd * sizeof(struct mem_range_desc), 306 M_MEMDESC, M_WAITOK); 307 error = mem_range_attr_get(md, &nd); 308 if (!error) 309 error = copyout(md, mo->mo_desc, 310 nd * sizeof(struct mem_range_desc)); 311 free(md, M_MEMDESC); 312 } 313 else 314 nd = mem_range_softc.mr_ndesc; 315 mo->mo_arg[0] = nd; 316 break; 317 318 case MEMRANGE_SET: 319 md = (struct mem_range_desc *)malloc(sizeof(struct mem_range_desc), 320 M_MEMDESC, M_WAITOK); 321 error = copyin(mo->mo_desc, md, sizeof(struct mem_range_desc)); 322 /* clamp description string */ 323 md->mr_owner[sizeof(md->mr_owner) - 1] = 0; 324 if (error == 0) 325 error = mem_range_attr_set(md, &mo->mo_arg[0]); 326 free(md, M_MEMDESC); 327 break; 328 } 329 return (error); 330 } 331