2 * Copyright (c) 1988 University of Utah.
3 * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
35 * from: Utah $Hdr: mem.c 1.13 89/10/08$
36 * from: @(#)mem.c 7.2 (Berkeley) 5/9/91
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
48 #include <sys/fcntl.h>
49 #include <sys/kernel.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>
57 #include <sys/msgbuf.h>
58 #include <sys/systm.h>
59 #include <sys/signalvar.h>
62 #include <machine/md_var.h>
63 #include <machine/vmparam.h>
67 #include <vm/vm_extern.h>
68 #include <vm/vm_page.h>
70 #include <machine/memdev.h>
72 static void ppc_mrinit(struct mem_range_softc *);
73 static int ppc_mrset(struct mem_range_softc *, struct mem_range_desc *, int *);
75 MALLOC_DEFINE(M_MEMDESC, "memdesc", "memory range descriptors");
77 static struct mem_range_ops ppc_mem_range_ops = {
83 struct mem_range_softc mem_range_softc = {
90 memrw(struct cdev *dev, struct uio *uio, int flags)
94 vm_offset_t va, eva, off, v;
105 while (uio->uio_resid > 0 && !error) {
107 if (iov->iov_len == 0) {
110 if (uio->uio_iovcnt < 0)
114 if (dev2unit(dev) == CDEV_MINOR_MEM) {
115 kmem_direct_mapped: v = uio->uio_offset;
117 off = uio->uio_offset & PAGE_MASK;
118 cnt = PAGE_SIZE - ((vm_offset_t)iov->iov_base &
120 cnt = min(cnt, PAGE_SIZE - off);
121 cnt = min(cnt, iov->iov_len);
123 if (mem_valid(v, cnt)) {
128 if (!pmap_dev_direct_mapped(v, cnt)) {
129 error = uiomove((void *)v, cnt, uio);
131 m.phys_addr = trunc_page(v);
133 error = uiomove_fromphys(&marr, off, cnt, uio);
136 else if (dev2unit(dev) == CDEV_MINOR_KMEM) {
137 va = uio->uio_offset;
139 if ((va < VM_MIN_KERNEL_ADDRESS) || (va > virtual_end))
140 goto kmem_direct_mapped;
142 va = trunc_page(uio->uio_offset);
143 eva = round_page(uio->uio_offset
147 * Make sure that all the pages are currently resident
148 * so that we don't create any zero-fill pages.
151 for (; va < eva; va += PAGE_SIZE)
152 if (pmap_extract(kernel_pmap, va) == 0)
155 prot = (uio->uio_rw == UIO_READ)
156 ? VM_PROT_READ : VM_PROT_WRITE;
158 va = uio->uio_offset;
159 if (kernacc((void *) va, iov->iov_len, prot)
163 error = uiomove((void *)va, iov->iov_len, uio);
173 * allow user processes to MMAP some memory sections
174 * instead of going through read/write
177 memmmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
178 int prot, vm_memattr_t *memattr)
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
188 if (dev2unit(dev) != CDEV_MINOR_MEM)
191 /* Only direct-mapped addresses. */
192 if (mem_valid(offset, 0)
193 && pmap_dev_direct_mapped(offset, 0))
198 for (i = 0; i < mem_range_softc.mr_ndesc; i++) {
199 if (!(mem_range_softc.mr_desc[i].mr_flags & MDF_ACTIVE))
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 &
208 *memattr = VM_MEMATTR_WRITE_BACK;
210 case MDF_WRITECOMBINE:
211 *memattr = VM_MEMATTR_WRITE_COMBINING;
213 case MDF_UNCACHEABLE:
214 *memattr = VM_MEMATTR_UNCACHEABLE;
216 case MDF_WRITETHROUGH:
217 *memattr = VM_MEMATTR_WRITE_THROUGH;
229 ppc_mrinit(struct mem_range_softc *sc)
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__);
240 ppc_mrset(struct mem_range_softc *sc, struct mem_range_desc *desc, int *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;
252 if (sc->mr_desc[i].mr_base == desc->mr_base &&
253 sc->mr_desc[i].mr_len == desc->mr_len)
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]));
273 * Operations for changing memory attributes.
275 * This is basically just an ioctl shim for mem_range_attr_get
276 * and mem_range_attr_set.
280 memioctl(struct cdev *dev __unused, u_long cmd, caddr_t data, int flags,
284 struct mem_range_op *mo = (struct mem_range_op *)data;
285 struct mem_range_desc *md;
287 /* is this for us? */
288 if ((cmd != MEMRANGE_GET) &&
289 (cmd != MEMRANGE_SET))
292 /* any chance we can handle this? */
293 if (mem_range_softc.mr_op == NULL)
296 /* do we have any descriptors? */
297 if (mem_range_softc.mr_ndesc == 0)
302 nd = imin(mo->mo_arg[0], mem_range_softc.mr_ndesc);
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);
309 error = copyout(md, mo->mo_desc,
310 nd * sizeof(struct mem_range_desc));
314 nd = mem_range_softc.mr_ndesc;
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;
325 error = mem_range_attr_set(md, &mo->mo_arg[0]);